Spring文档苦读【2】【在容器外在定义用户对象进容器】

前言

有时我们需要自定义对象,不用Spring来管理我们的对象创建,以便我们在创建过程中做一些其他操作。

一般Spring创建我们对象有两种,一种是使用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     xsi:schemaLocation="http://www.springframework.org/schema/beans
 5         http://www.springframework.org/schema/beans/spring-beans.xsd">
 6 
 7     <bean id="accountDao"
 8         class="org.springframework.samples.jpetstore.dao.jpa.JpaAccountDao">
 9         <!-- additional collaborators and configuration for this bean go here -->
10     </bean>
11 
12     <bean id="itemDao" class="org.springframework.samples.jpetstore.dao.jpa.JpaItemDao">
13         <!-- additional collaborators and configuration for this bean go here -->
14     </bean>
15 
16     <!-- more bean definitions for data access objects go here -->
17 
18 </beans>
View Code

一种是通过,注解的方式。如:@Service,@Component,@Repository等

下面我们就讲解一下如何让用户自定义的创建Bean,并注册到我们的Spring容器中。

用户Bean对象

 1 public class UserBean {
 2     private String name="My User Bean";
 3 
 4     public String getName() {
 5         return name;
 6     }
 7 
 8     public void setName(String name) {
 9         this.name = name;
10     }
11     
12 }
UserBean

对象装配并获取测试

 1 @RunWith(SpringRunner.class)
 2 @ContextConfiguration("/applicationContext.xml")
 3 public class UserBeanTest {
 4     @Autowired
 5     private ConfigurableApplicationContext applicationContext;
 6     @Before
 7     public void registerUserBeanTest() {
 8         DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory)applicationContext.getBeanFactory();
 9         RootBeanDefinition beanDefinition = new RootBeanDefinition(UserBean.class);
10         beanDefinition.setSource(new UserBean());
11         /**
12          * ConfigurableBeanFactory.SCOPE_SINGLETON(默认)
13          * 单例模式,每次获取同一个对象
14          * ConfigurableBeanFactory.SCOPE_PROTOTYPE
15          * 这个是说在每次注入的时候回自动创建一个新的bea
16          */
17         beanDefinition.setScope(ConfigurableBeanFactory.SCOPE_SINGLETON);//
18         beanDefinition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);//表示该类为基础设施类,防止该bean被代理
19         beanDefinition.getPropertyValues().add("name", "your user bean");
20         beanFactory.registerBeanDefinition("userBean", beanDefinition);
21     }
22     
23     @Test
24     public void getUserBeanTest() {
25         UserBean user = applicationContext.getBean(UserBean.class);
26         UserBean user1 = applicationContext.getBean(UserBean.class);
27         Assert.assertEquals(user.getName(), "your user bean");
28         System.out.println(user.getName());
29         System.out.println(user == user1);
30     }
31 }

applicationContext.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:oxm="http://www.springframework.org/schema/oxm"
 5     xmlns:mvc="http://www.springframework.org/schema/mvc"
 6     xmlns:p="http://www.springframework.org/schema/p"
 7     xmlns:context="http://www.springframework.org/schema/context"
 8     xmlns:task="http://www.springframework.org/schema/task"
 9     xmlns:aop="http://www.springframework.org/schema/aop"
10     xsi:schemaLocation="http://www.springframework.org/schema/beans 
11     http://www.springframework.org/schema/beans/spring-beans.xsd 
12     http://www.springframework.org/schema/oxm 
13     http://www.springframework.org/schema/oxm/spring-oxm.xsd
14     http://www.springframework.org/schema/context
15     http://www.springframework.org/schema/context/spring-context.xsd
16     http://www.springframework.org/schema/mvc
17     http://www.springframework.org/schema/mvc/spring-mvc.xsd
18     http://www.springframework.org/schema/task
19     http://www.springframework.org/schema/task/spring-task-4.3.xsd
20     http://www.springframework.org/schema/aop
21     http://www.springframework.org/schema/aop/spring-aop.xsd">
22     <context:component-scan base-package="com.may.web,com.may.aspect">
23     </context:component-scan>
24     <aop:aspectj-autoproxy>
25     </aop:aspectj-autoproxy>
26     <mvc:annotation-driven />
27 </beans>

 

posted on 2016-12-22 18:03  源码解析  阅读(126)  评论(0)    收藏  举报

导航