Spring--通过注解来配置bean【转】
Spring通过注解配置bean
TestObject.java 1 package com.yl.annotation; 2 3 import org.springframework.stereotype.Component; 4 5 @Component 6 public class TestObject { 7 8 } UserRepository.java接口 1 package com.yl.annotation.repository; 2 3 public interface UserRepository { 4 public void save(); 5 6 } UserRepositoryImpl.java 1 package com.yl.annotation.repository; 2 3 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.stereotype.Repository; 5 6 import com.yl.annotation.TestObject; 7 8 @Repository 9 //@Repository("userRepository") 10 public class UserRepositoryImpl implements UserRepository { 11 12 @Autowired(required=false) 13 private TestObject testObject; 14 15 @Override 16 public void save() { 17 System.out.println("UserRepository save..."); 18 System.out.println(testObject); 19 } 20 21 } UserJdbcRepository.java 1 package com.yl.annotation.repository; 2 3 import org.springframework.stereotype.Repository; 4 5 @Repository 6 public class UserJdbcRepository implements UserRepository { 7 8 @Override 9 public void save() { 10 System.out.println("UserJdbcRepository save..."); 11 } 12 13 } UserService.java 1 package com.yl.annotation.service; 2 3 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.beans.factory.annotation.Qualifier; 5 import org.springframework.stereotype.Service; 6 7 import com.yl.annotation.repository.UserRepository; 8 9 @Service 10 public class UserService { 11 @Autowired 12 @Qualifier("userJdbcRepository") 13 private UserRepository userRepository; 14 15 /*@Autowired 16 @Qualifier("userJdbcRepository") 17 public void setUserRepository(UserRepository userRepository) { 18 this.userRepository = userRepository; 19 }*/ 20 21 public void add() { 22 System.out.println("UserService add..."); 23 userRepository.save(); 24 } 25 } UserController.java 1 package com.yl.annotation.controller; 2 3 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.stereotype.Controller; 5 6 import com.yl.annotation.service.UserService; 7 8 @Controller 9 public class UserController { 10 @Autowired 11 private UserService userService; 12 13 public void execute() { 14 System.out.println("UserController execute..."); 15 userService.add(); 16 } 17 } beans-annotation.xml 1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 6 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd"> 7 8 <!-- 指定Spring IOC容器扫描的包 --> 9 <!-- 可以通过resource-pattern指定扫描的资源 --> 10 <!-- <context:component-scan 11 base-package="com.yl.annotation" 12 resource-pattern="repository/*.class"></context:component-scan> --> 13 14 <!-- context:exclude-filter 子节点指定排除哪些指定表达式的组件 --> 15 <!-- context:include-filter 子节点指定包含哪些指定表达式的组件, 该子节点需要use-default-filters配合使用 --> 16 <context:component-scan 17 base-package="com.yl.annotation" > 18 <!-- use-default-filters="false"> --> 19 <!-- <context:exclude-filter type="annotation" 20 expression="org.springframework.stereotype.Repository"/> --> 21 22 <!-- <context:include-filter type="annotation" 23 expression="org.springframework.stereotype.Repository"/> --> 24 25 <!-- <context:exclude-filter type="assignable" 26 expression="com.yl.annotation.repository.UserRepository"/> --> 27 28 <!-- <context:include-filter type="annotation" 29 expression="com.yl.annotation.repository.UserRepository"/> --> 30 </context:component-scan> 31 </beans> ? 使用@Resource或@Inject自动装配bean (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- ThinkPHP提示错误Fatal error: Allowed memory size的解决方
- 如何进行大数据分析及处理?
- 在Delphi中使用TList时是否必须分配和释放记录?
- Golang学习摘录(二)
- Delphi调用C#写的webservice
- perl – 我如何整理DBIx :: Class :: Schema :: Loader的输
- Groovy ShortTypeHandling ClassNotFoundException
- Perl的特殊符号
- delphi2007下使用indy连接https时could not load ssl libra
- DELPHI语法基础学习笔记-Windows 句柄、回调函数、函数重载