| |
| <?xml version="1.0" encoding="UTF-8"?> |
| <beans xmlns="http://www.springframework.org/schema/beans" |
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| xmlns:context="http://www.springframework.org/schema/context" |
| xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd |
| http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> |
| |
| <context:component-scan base-package="com.ychen"></context:component-scan> |
| |
| </beans> |
| |
| |
| public interface UserDao { |
| |
| public void add(); |
| |
| } |
| |
| |
| @Repository |
| public class UserDaoImpl implements UserDao { |
| |
| @Override |
| public void add() { |
| System.out.println("dao add....."); |
| } |
| |
| } |
| |
| |
| @Service |
| public class UserService { |
| |
| @Autowired |
| private UserDao userDao; |
| |
| public void add() { |
| System.out.println("service add......."); |
| userDao.add(); |
| } |
| |
| } |
| |
| |
| public class Test3 { |
| |
| @Test |
| public void testAdd() { |
| ApplicationContext context = new ClassPathXmlApplicationContext("bean7.xml"); |
| UserService userService = context.getBean("userService", UserService.class); |
| System.out.println(userService); |
| userService.add(); |
| } |
| |
| } |
| |
| |
| com.ychen.spring.ser.UserService@37911f88 |
| service add....... |
| dao add..... |
| |
| Process finished with exit code 0 |
- @Qualifier:根据名称进行注入,需配合@Autowired一起使用
| |
| <?xml version="1.0" encoding="UTF-8"?> |
| <beans xmlns="http://www.springframework.org/schema/beans" |
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| xmlns:context="http://www.springframework.org/schema/context" |
| xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd |
| http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> |
| |
| <context:component-scan base-package="com.ychen"></context:component-scan> |
| |
| </beans> |
| |
| |
| public interface UserDao { |
| |
| public void add(); |
| |
| } |
| |
| |
| @Repository(value = "userDaoImpl1") |
| public class UserDaoImpl implements UserDao { |
| |
| @Override |
| public void add() { |
| System.out.println("dao add....."); |
| } |
| |
| } |
| |
| |
| @Service |
| public class UserService { |
| |
| @Autowired |
| @Qualifier(value = "userDaoImpl1") |
| private UserDao userDao; |
| |
| public void add() { |
| System.out.println("service add......."); |
| userDao.add(); |
| } |
| |
| } |
| |
| |
| public class Test3 { |
| |
| @Test |
| public void testAdd() { |
| ApplicationContext context = new ClassPathXmlApplicationContext("bean7.xml"); |
| UserService userService = context.getBean("userService", UserService.class); |
| System.out.println(userService); |
| userService.add(); |
| } |
| |
| } |
| |
| |
| com.ychen.spring.ser.UserService@37911f88 |
| service add....... |
| dao add..... |
| |
| Process finished with exit code 0 |
- @Resource:可以根据类型注入,可以根据名称注入,该注解并不时spring提供的注解,只是java扩展包中的注解
| |
| |
| <?xml version="1.0" encoding="UTF-8"?> |
| <beans xmlns="http://www.springframework.org/schema/beans" |
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| xmlns:context="http://www.springframework.org/schema/context" |
| xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd |
| http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> |
| |
| <context:component-scan base-package="com.ychen"></context:component-scan> |
| |
| </beans> |
| |
| |
| public interface UserDao { |
| |
| public void add(); |
| |
| } |
| |
| |
| @Repository(value = "userDaoImpl1") |
| public class UserDaoImpl implements UserDao { |
| |
| @Override |
| public void add() { |
| System.out.println("dao add....."); |
| } |
| |
| } |
| |
| |
| @Service |
| public class UserService { |
| |
| @Resource |
| private UserDao userDao; |
| |
| public void add() { |
| System.out.println("service add......."); |
| userDao.add(); |
| } |
| |
| } |
| |
| |
| public class Test3 { |
| |
| @Test |
| public void testAdd() { |
| ApplicationContext context = new ClassPathXmlApplicationContext("bean7.xml"); |
| UserService userService = context.getBean("userService", UserService.class); |
| System.out.println(userService); |
| userService.add(); |
| } |
| |
| } |
| |
| |
| com.ychen.spring.ser.UserService@37911f88 |
| service add....... |
| dao add..... |
| |
| Process finished with exit code 0 |
| |
| |
| |
| <?xml version="1.0" encoding="UTF-8"?> |
| <beans xmlns="http://www.springframework.org/schema/beans" |
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| xmlns:context="http://www.springframework.org/schema/context" |
| xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd |
| http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> |
| |
| <context:component-scan base-package="com.ychen"></context:component-scan> |
| |
| </beans> |
| |
| |
| public interface UserDao { |
| |
| public void add(); |
| |
| } |
| |
| |
| @Repository(value = "userDaoImpl1") |
| public class UserDaoImpl implements UserDao { |
| |
| @Override |
| public void add() { |
| System.out.println("dao add....."); |
| } |
| |
| } |
| |
| |
| @Service |
| public class UserService { |
| |
| @Resource(name = "userDaoImpl1") |
| private UserDao userDao; |
| |
| public void add() { |
| System.out.println("service add......."); |
| userDao.add(); |
| } |
| |
| } |
| |
| |
| public class Test3 { |
| |
| @Test |
| public void testAdd() { |
| ApplicationContext context = new ClassPathXmlApplicationContext("bean7.xml"); |
| UserService userService = context.getBean("userService", UserService.class); |
| System.out.println(userService); |
| userService.add(); |
| } |
| |
| } |
| |
| |
| com.ychen.spring.ser.UserService@37911f88 |
| service add....... |
| dao add..... |
| |
| Process finished with exit code 0 |
| |
| <?xml version="1.0" encoding="UTF-8"?> |
| <beans xmlns="http://www.springframework.org/schema/beans" |
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| xmlns:context="http://www.springframework.org/schema/context" |
| xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd |
| http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> |
| |
| <context:component-scan base-package="com.ychen"></context:component-scan> |
| |
| </beans> |
| |
| |
| public interface UserDao { |
| |
| public void add(); |
| |
| } |
| |
| |
| @Repository |
| public class UserDaoImpl implements UserDao { |
| |
| @Override |
| public void add() { |
| System.out.println("dao add....."); |
| } |
| |
| } |
| |
| |
| @Service |
| public class UserService { |
| |
| @Value(value = "abc") |
| private String name; |
| |
| @Autowired |
| private UserDao userDao; |
| |
| public void add() { |
| System.out.println("service add......." + name); |
| userDao.add(); |
| } |
| |
| } |
| |
| |
| public class Test3 { |
| |
| @Test |
| public void testAdd() { |
| ApplicationContext context = new ClassPathXmlApplicationContext("bean7.xml"); |
| UserService userService = context.getBean("userService", UserService.class); |
| System.out.println(userService); |
| userService.add(); |
| } |
| |
| } |
| |
| |
| com.ychen.spring.ser.UserService@2cb2fc20 |
| service add.......abc |
| dao add..... |
| |
| Process finished with exit code 0 |
| # 编写配置类 |
| @Configuration |
| @ComponentScan(basePackages = {"com.ychen"}) |
| public class SpringConfig { |
| |
| } |
| |
| # 编写测试方法 |
| @Test |
| public void testService2() { |
| ApplicationContext context |
| = new AnnotationConfigApplicationContext(SpringConfig.class); |
| UserService userService = context.getBean("userService", UserService.class); |
| System.out.println(userService); |
| userService.add(); |
| } |
| |
| # 控制台 |
| com.ychen.spring.ser.UserService@7975d1d8 |
| service add.......abc |
| dao add..... |
| |
| Process finished with exit code 0 |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术