1 修改配置文件
applicationContext.xml
增加命名空间的和约束文件
<beans ...
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
2.开启Spring的注解功能
<!-- 扫描注解类
base-package : 表示当前扫描的包,框架会根据包查找所有使用注解的类
-->
<context:component-scan base-package="com.xundh.test.spring.*" />
这里可能会遇到代码有提示:
advised by org.springframework.transaction.interceptor.TransactionIntercepto
提示spring事务管理异常,处理方法是:
<context:component-scan base-package="com.cn.test.services" use-default-filters="false"/>
参考:http://www.oschina.net/question/2272241_2146828?sort=time
然而直接加可能又会遇到hibernate失效的问题。
原因:
1. <context:component-scan>会交给org.springframework.context.config.ContextNamespaceHandler处理:
registerBeanDefinitionParser("component-scan", new ComponentScanBeanDefinitionParser());
2. <context:component-scan>的use-default-filters默认true,会交给ClassPathScanningCandidateComponentProvider进行扫描,
protected void registerDefaultFilters() {
this.includeFilters.add(new AnnotationTypeFilter(Component.class));
ClassLoader cl = ClassPathScanningCandidateComponentProvider.class.getClassLoader();
try {
this.includeFilters.add(new AnnotationTypeFilter(
((Class<? extends Annotation>) cl.loadClass("javax.annotation.ManagedBean")), false));
logger.debug("JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning");
}
catch (ClassNotFoundException ex) {
// JSR-250 1.1 API (as included in Java EE 6) not available - simply skip.
}
try {
this.includeFilters.add(new AnnotationTypeFilter(
((Class<? extends Annotation>) cl.loadClass("javax.inject.Named")), false));
logger.debug("JSR-330 'javax.inject.Named' annotation found and supported for component scanning");
}
catch (ClassNotFoundException ex) {
// JSR-330 API not available - simply skip.
}
}
可以看到默认ClassPathBeanDefinitionScanner会自动注册对@Component、@ManagedBean、@Named注解的Bean进行扫描。如果细心,到此就找到问题根源了。
3. 对exclude-filter,include-filter进行过滤
首先通过exclude-filter 进行黑名单过滤;
然后通过include-filter 进行白名单过滤;
否则默认排除。
所以,<context:component-scan这段代码不仅仅对@Controller的注解Bean进行了扫描,而且对@Comonent注解也扫描了,子注解@Service,@Repository也进行了扫描,所以原来mvc的上下文的Bean被后面的bean替换了。所以如下解决方法:
- 如果不需要默认的,则use-default-filters=“false”禁用掉。
- 直接写明具体Controller包名称。
示例:
<context:component-scan base-package="com.cn.test.services" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
<context:include-filter type="annotation" expression="org.springframework.stereotype.Component" />
<context:include-filter type="annotation" expression="org.springframework.transaction.annotation.Transactional" />
<context:include-filter type="annotation" expression="javax.ws.rs.ext.Provider" />
</context:component-scan>
参考:
http://blog.csdn.net/u014520797/article/details/50667720
3.声明注解类
类的分层注解
Spring框架提供了三种对于不同层的注解方式:
- 控制层:@Controller
- 服务层:@Service
- 持久层:@Repository
类似于配置文件中的:
<bean id="orgService" class="com.xundh.web.Service.TestService"></bean>
默认情况下使用注解声明的Bean对象id名称为:类名称的首字母小写,
一般不需要我们进行设置,如果想要设置时,如我们指定的实现类,而名字指向了接口,直接添加value属性即可:
@Service(value=”name”)。 value也是可以省略的。
Spring框架还为我们提供了代替 边三个分层注解的通用注解:@Component。
自动装配功能的实现对象之间的组合关系
- @Autowired : 采用类型的方式完成自动装配 : byType
- @Resource : 采用名称+类型的方式完成自动装配 : byName +byType,此种方法推荐使用。
另外Spring提供的两种注解的自动装配功能,属性不需要声明set,get方法也可以完成组合功能,这是非常方便的。
在声明action时,需要指定其为多例的,解决线程安全的问题,在配置文件中我们通常会这样声明action的类
<bean id="orgAction" class="com.ljh.web.action.OrgAction" scope="prototype"></bean>
而在Spring注解开发中也提供了类似的属性来解决此问题,在action类前边设定如下:
@Scope("prototype")
利用配置文件声明Dao层类时,通常是这样的
<!-- dao层需要引用我们的sessionFactory工厂,自动装配时,
是会自动装配名叫sessionFactory的工厂,所以可以省略。
-->
<bean name ="orgTypeDao" class="com.ljh.dao.impl.OrgTypeDaoImpl">
<!--<property name="sessionFactory" ref="sessionFactory"></property>-->
</bean>
注解开发的方法如何进行sessionFactory注入:
Dao层从HibernateDaoSupport继承,父类有一个
//父类中的sessionFactory的set注入方法
public ublic final void setSessionFactory(SessionFactory sessionFactory) {
if (this.hibernateTemplate == null || sessionFactory != this.hibernateTemplate.getSessionFactory()) {
this.hibernateTemplate = createHibernateTemplate(sessionFactory);
}
方法,这是sessionFactroy的set注入应运。如果重写此方法,在此方法上进行@Resource注解,即可解决我们的问题,但是可以看到此方法是用final修饰的,所以通常情况下,使用下边这种方法进行解决:
@Repository("orgDao")
public class OrgDaoImpl extends HibernateDaoSupport implements OrgDao {
@Resource
//自定义了方法,解决了父类中final修饰不能重写的问题
public void setSuperSessionFactory(SessionFactory sessionFactory) {
//调用了父类的setSessionFactory方法
super.setSessionFactory(sessionFactory);
}
}
这样,Dao层的sessionFactory就进行了注入,我们可以正常编写我们的Dao层代码了。
转载自:
http://blog.csdn.net/liujiahan629629/article/details/22696031