# demo_spring05_di_annotation **Repository Path**: JavaObjects/demo_spring05_di_annotation ## Basic Information - **Project Name**: demo_spring05_di_annotation - **Description**: No description available - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2019-09-27 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Java-Spring基于注解的方式给依赖属性赋值 *沿用上一个项目* 1. 注释掉属性的set/get方法,并运行测试,如图所示测试失败  **结论:** 根据类型自动装配还是需要配置属性的get/set方法 2. 还原被注释掉的属性的set/get方法 3. 新建一个Java Project命名为 demo_spring05_di_annotation 4. 导入对应的jar包  5. 写对应层的类 以及配置beans.xml **MyController** ``` package test; public class MyController { private MyServiceIfac service; public MyController(MyServiceIfac myService) { this.service = myService; } public void login() { System.out.println("MyController login........"); service.serviceLogin(); } } ``` **MyDao** ``` package test; public class MyDao implements MyDaoIfac { /* (non-Javadoc) *
Title: queryUserByNameAndPwd
*Description:
* @see test.MyDaoIfac#queryUserByNameAndPwd() */ @Override public void queryUserByNameAndPwd() { System.out.println("MyDao queryUserByNameAndPwd"); } } ``` **MyDaoIfac** ``` package test; public interface MyDaoIfac { void queryUserByNameAndPwd(); } ``` **MyService** ``` package test; public class MyService implements MyServiceIfac { private MyDaoIfac dao;//MyService依赖MyDao /** * 为咯给属性赋值 就在属性所在的 * 类里面设置一个构造方法 * 并且该构造方法接收一个该属性类开的参数 *Title:
*Description:
* @param myDao */ public MyService(MyDaoIfac myDao) { this.dao = myDao; } /* (non-Javadoc) *Title: serviceLogin
*Description:
* @see test.MyServiceIfac#serviceLogin() */ @Override public void serviceLogin() { System.out.println("MyService serviceLogin()......"); dao.queryUserByNameAndPwd(); } } ``` **MyService** ``` package test; public class MyService implements MyServiceIfac { private MyDaoIfac dao;//MyService依赖MyDao /** * 为咯给属性赋值 就在属性所在的 * 类里面设置一个构造方法 * 并且该构造方法接收一个该属性类开的参数 *Title:
*Description:
* @param myDao */ public MyService(MyDaoIfac myDao) { this.dao = myDao; } /* (non-Javadoc) *Title: serviceLogin
*Description:
* @see test.MyServiceIfac#serviceLogin() */ @Override public void serviceLogin() { System.out.println("MyService serviceLogin()......"); dao.queryUserByNameAndPwd(); } } ``` **MyServiceIfac** ``` package test; public interface MyServiceIfac { void serviceLogin(); } ``` **Test测试类** ``` package test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) { //实例化spring容器及bean ApplicationContext act = new ClassPathXmlApplicationContext("beans.xml"); // 得到的是此处的controller //Title: login
*Description:
*/ // public MyController(MyServiceIfac myService) // { // this.service = myService; // } public void login() { System.out.println("MyController login........"); service.serviceLogin(); } } ``` **MyDao** ``` package test; import org.springframework.stereotype.Repository; //又有接口又有实现类 帽子给实现类带 @Repository public class MyDao implements MyDaoIfac { /* (non-Javadoc) *Title: queryUserByNameAndPwd
*Description:
* @see test.MyDaoIfac#queryUserByNameAndPwd() */ @Override public void queryUserByNameAndPwd() { System.out.println("MyDao queryUserByNameAndPwd"); } } ``` **MyDaoIfac** ``` package test; public interface MyDaoIfac { void queryUserByNameAndPwd(); } ``` **MyService** ``` package test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;//这是一个注解 @Service public class MyService implements MyServiceIfac { @Autowired private MyDaoIfac dao;//MyService依赖MyDao /** * 为咯给属性赋值 就在属性所在的 * 类里面设置一个构造方法 * 并且该构造方法接收一个该属性类开的参数 *Title:
*Description:
* @param myDao */ // public MyService(MyDaoIfac myDao) // { // this.dao = myDao; // } /* (non-Javadoc) *Title: serviceLogin
*Description:
* @see test.MyServiceIfac#serviceLogin() */ @Override public void serviceLogin() { System.out.println("MyService serviceLogin()......"); dao.queryUserByNameAndPwd(); } } ``` **MyServiceIfac** ``` package test; public interface MyServiceIfac { void serviceLogin(); } ``` **Test类** ``` package test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) { //实例化spring容器及bean ApplicationContext act = new ClassPathXmlApplicationContext("beans.xml"); // 得到的是此处的controller //