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); } }
相关推荐
在Spring1.2或之前的版本中,实现AOP的传统方式就是通过实现Spring的AOP API来定义Advice,并设置代理对象。Spring根据Adivce加入到业务流程的时机的不同,提供了四种不同的Advice:Before Advice、After Advice、...
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> <!-- apache.dbcp连接池的配置 --> <bean id="dataSource" class="org.apache.commons.dbcp....
web.xml 中定义了一个名为 annomvc 的 Spring MVC 模块,按照 Spring MVC 的契约,需要在 WEB-INF/annomvc-servlet.xml 配置文件中定义 Spring MVC 模块的具体配置。annomvc-servlet.xml 的配置内容如下所示: <?xml...
application.xml配置 ... <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> </beans>
在Spring3中配置数据源,包括DBCP,C3P0,Proxool,Bonecp主要的数据源,里面包含这些数据源的jar文件和依赖文件及配置文件。。 如Bonecp目前听说是最快的数据源,速度是传统的c3p0的25倍, bonecp.properties文件: ...
nested exception is java.lang.NoClassDefFoundError: org/aspectj/lang/annotation/Around 由于缺少依赖包 aopalliance-1.0.jar org.springframework.beans.factory.BeanCreationException: Error creating bean...
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" p:order="0"> <property name="interceptors"> <list> <ref bean="doubleSubmitInterceptor"/> </list> ...
14. <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> 15. 16. <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 --> 17. <bean class="org....
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <ref bean="dataSource"></ref> </property> <prop key="hibernate.dialect">org.hibernate....
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <!-- 匹配...
如何向Spring Bean中注入一个JavautilProperties 请解释Spring Bean的自动装配 请解释自动装配模式的区别 如何开启基于注解的自动装配 请举例解释Required annotation 请举例解释Autowired注解 请举例说明Qualifier...
使用Maven POM editor打开项目中的pom.xml文件,选择Dependencies,在Dependencies栏目点击Add进行,首先弹出一个搜索按钮,例如输入spring-web,就会自动搜索关于spring-web相关的jar包,我们选择3.0.5版本的spring...
2:在springweb.xml中加入: <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <ref bean="mappingJacksonHttpMessageConverter" /> <ref bean=...
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 容器启动时,AutowiredAnnotationBeanPostProcessor 将扫描 Spring 容器中所有 Bean,当发现 Bean 中拥有 @Autowired 注释时就找到和其匹配(默认按类型匹配)的 Bean,并注入到对应的地方中去。...
<bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor" /> <!-- 使用 annotation 自动注册bean,并检查@Controller, @Service, @Repository注解已被注入,也可以...