`

spring中bean的范围(xml/annotation)

 
阅读更多

1.xml

配置文件bean.xml中,bean标签中的属性scope默认为singleton,表示每次创建的service和首次创建的是同一个对象,如果设置为prototype,表示每次创建的service是不同的对象。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

	<bean id="userDAOImpl" class="com.test.dao.impl.UserDAOImpl"></bean>
	<bean id="userService" class="com.test.service.UserService" scope="prototype">
		<property name="userDAO">
			<ref bean="userDAOImpl" />
		</property>
	</bean>
	
</beans>

 

2.annotation

UserDAOImpl.java

package com.test.dao.impl;

import org.springframework.stereotype.Component;

import com.test.dao.UserDAO;
import com.test.model.User;

@Component("userDAOImpl")
public class UserDAOImpl implements UserDAO {
	public void save(User user) {
		System.out.println("user saved!");
	}
}

 

UserService.java

package com.test.service;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.test.dao.UserDAO;
import com.test.model.User;

@Scope("prototype")
@Component("userService")
public class UserService {
	private UserDAO userDAO;

	public UserDAO getUserDAO() {
		return userDAO;
	}

	@Resource(name="userDAOImpl")
	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}

	public void add(User user) {
		userDAO.save(user);
	}

}

 

公共junit测试类

UserServiceTest.java

package com.test.service;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.test.model.User;

public class UserServiceTest {

	@Test
	public void testAdd() throws Exception {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
		UserService service = (UserService) applicationContext.getBean("userService");
		UserService service2 = (UserService) applicationContext.getBean("userService");
		//scope默认是singleton(单例的意思),这里打印值为true
		//scope设置为prototype(原型的意思),每次创建新的对象,这里打印值为false
		System.out.println(service == service2);
		User u = new User();
		u.setUsername("zhangsan");
		u.setPassword("123");
		service.add(u);
	}

}

 

分享到:
评论

相关推荐

    spring aop 实现源代码--xml and annotation(带lib包)

    在Spring1.2或之前的版本中,实现AOP的传统方式就是通过实现Spring的AOP API来定义Advice,并设置代理对象。Spring根据Adivce加入到业务流程的时机的不同,提供了四种不同的Advice:Before Advice、After Advice、...

    springjdbc

    &lt;bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /&gt; &lt;!-- apache.dbcp连接池的配置 --&gt; &lt;bean id="dataSource" class="org.apache.commons.dbcp....

    springweb3.0MVC注解(附实例)

    web.xml 中定义了一个名为 annomvc 的 Spring MVC 模块,按照 Spring MVC 的契约,需要在 WEB-INF/annomvc-servlet.xml 配置文件中定义 Spring MVC 模块的具体配置。annomvc-servlet.xml 的配置内容如下所示: &lt;?xml...

    struts2.3+hibernate3.6+spring3.1整合的纯xml配置的小项目

    application.xml配置 ... &lt;bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"&gt; &lt;property name="sessionFactory" ref="sessionFactory"&gt;&lt;/property&gt; &lt;/bean&gt; &lt;/beans&gt;

    Spring3中配置DBCP,C3P0,Proxool,Bonecp数据源

    在Spring3中配置数据源,包括DBCP,C3P0,Proxool,Bonecp主要的数据源,里面包含这些数据源的jar文件和依赖文件及配置文件。。 如Bonecp目前听说是最快的数据源,速度是传统的c3p0的25倍, bonecp.properties文件: ...

    springAOP demo 带错误解决文档

    nested exception is java.lang.NoClassDefFoundError: org/aspectj/lang/annotation/Around 由于缺少依赖包 aopalliance-1.0.jar org.springframework.beans.factory.BeanCreationException: Error creating bean...

    spring applicationContext 配置文件

    &lt;bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" p:order="0"&gt; &lt;property name="interceptors"&gt; &lt;list&gt; &lt;ref bean="doubleSubmitInterceptor"/&gt; &lt;/list&gt; ...

    spring_MVC源码

    14. &lt;bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /&gt; 15. 16. &lt;!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 --&gt; 17. &lt;bean class="org....

    维生药业小项目 SSH简单学习项目

    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"&gt; &lt;ref bean="dataSource"&gt;&lt;/ref&gt; &lt;/property&gt; &lt;prop key="hibernate.dialect"&gt;org.hibernate....

    springmvc-ibatis

    &lt;bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt; &lt;property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /&gt; &lt;!-- 匹配...

    25个经典的Spring面试问答

    如何向Spring Bean中注入一个JavautilProperties 请解释Spring Bean的自动装配 请解释自动装配模式的区别 如何开启基于注解的自动装配 请举例解释Required annotation 请举例解释Autowired注解 请举例说明Qualifier...

    基于MyEclipse搭建maven+springmvc整合图文教程(含源码0

    使用Maven POM editor打开项目中的pom.xml文件,选择Dependencies,在Dependencies栏目点击Add进行,首先弹出一个搜索按钮,例如输入spring-web,就会自动搜索关于spring-web相关的jar包,我们选择3.0.5版本的spring...

    springweb-Jackson

    2:在springweb.xml中加入: &lt;bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"&gt; &lt;ref bean="mappingJacksonHttpMessageConverter" /&gt; &lt;ref bean=...

    Spring中文帮助文档

    6.4.2. Spring AOP中使用@AspectJ还是XML? 6.5. 混合切面类型 6.6. 代理机制 6.6.1. 理解AOP代理 6.7. 以编程方式创建@AspectJ代理 6.8. 在Spring应用中使用AspectJ 6.8.1. 在Spring中使用AspectJ进行domain ...

    spring基础

    这样,当 Spring 容器启动时,AutowiredAnnotationBeanPostProcessor 将扫描 Spring 容器中所有 Bean,当发现 Bean 中拥有 @Autowired 注释时就找到和其匹配(默认按类型匹配)的 Bean,并注入到对应的地方中去。...

    spring3.2+strut2+hibernate4

    &lt;bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor" /&gt; &lt;!-- 使用 annotation 自动注册bean,并检查@Controller, @Service, @Repository注解已被注入,也可以...

Global site tag (gtag.js) - Google Analytics