使用spring类注解简化配置

前言

xml文件以及注解都可以对我们java源文件起到辅助支持的作用,本例针对于@Controller @Service @Repository这四个spring的类注解进行详细讲解。首先:我们浏览一下我们原始的applicationContext.xml中的部分配置。

1
2
3
4
5
6
7
8
9
10
11
<bean id="myNewsAction" class="news.action.NewsAction" scope="prototype">
<property name="ns" ref="myNewsService"></property>
</bean>

<bean id="myNewsService" class="news.service.NewsServiceImpl" scope="prototype">
<property name="nd" ref="myNewsDao"></property>
</bean>

<bean id="myNewsDao" class="news.dao.NewsDaoImpl" scope="prototype">
<property name="sf" ref="mySessionFactory"></property>
</bean>

解析:当一个项目中所涉及到的java bean十分庞大,而每一个bean中的配置都是大同小异的,那么这份applicationContext.xml文件显得有些冗杂。

第一步简化

1
2
3
4
5
<bean id="myNewsAction" class="news.action.NewsAction" scope="prototype"></bean>

<bean id="myNewsService" class="news.service.NewsServiceImpl" scope="prototype"></bean>

<bean id="myNewsDao" class="news.dao.NewsDaoImpl" scope="prototype"></bean>

解析:在配置文件中只需申明这个bean,然后在源文件中添加@Autowired,@Qualifier这两个注解。数据层SessionFactory属性注入详情如下:

1
2
3
@Autowired
@Qualifier("mySessionFactory")
private SessionFactory sessionFactory;

解析:在@Qualifier这个注解中我们指定其引用的是哪一个bean,spring便会自动为其注入这个实例,并且属性的set方法也可省略
但是:经过上面的一番操作仿佛没有给我省多少事,别急,精华总是留在最后。

第二步简化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">

<!-- 基于news这个包自动扫描其中的类 ,也会自动注入解析器-->
<context:component-scan base-package="news"></context:component-scan>

<!-- 引入外部属性文件 -->
<context:property-placeholder location="classpath:jdbc.properties" />

<bean id="mySessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
</bean>

<bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">

</bean>

</beans>

解析:从这份applicationContext.xml文件中我们可以明显的看到并没有给我们的java bean进行相关配置,只是配置了一些基本的数据源。唯一多了一行:

1
<context:component-scan base-package="news"></context:component-scan>

通过这个节点的base-package属性可以配置spring需要自动注入的哪个基包。
此时便是spring的@Controller @Service @Repository这三个注解起作用的时候了。

1
2
3
4
5
6
7
@Controller("myNewsAction")
@Scope("prototype")
public class NewsAction extends ActionSupport {

@Autowired
@Qualifier("myNewsService")
private NewsService ns;

1
2
3
4
5
6
7
@Service("myNewsService")
@Scope("prototype")
public class NewsServiceImpl implements NewsService {

@Autowired
@Qualifier("myNewsDao")
private NewsDao nd;
1
2
3
4
5
6
7
@Repository("myNewsDao")
@Scope("prototype")
public class NewsDaoImpl implements NewsDao {

@Autowired
@Qualifier("mySessionFactory")
private SessionFactory sf;

解析:①,注解@Controller为我们的控制器action类的类注解相当于applicationContext.xml文件中的bean节点,而括号中的值相当于bean节点中的id属性的属性值。同理:@Service为我们业务层的类注解,@Repository为数据层dao的类注解。
②,注解 @Scope(“prototype”) 相当于applicationContext.xml文件中bean节点中scope属性,这个非单例模式注解十分重要,主要起到线程安全,防止并发操作时出现异常的作用。

小结

使用spring的类注解和属性注解确实能给我们带来许多便利,关于类属性的注解其实jdk javax.annotation.Resource包中便有@Resource注解。所以,我们当然也可以选择使用jdk的注解,不过要注意的是,千万不要把jdk的注解和spring的注解混用。在软件系统中,由于原生的jdk难免存在一些缺陷,我们在开发过程中往往需要引入各种框架,因此我们的项目便不得不与这些框架耦合在一起。虽然我们一直不希望我们的代码出现耦合,毕竟这只是一种理想状态。总之,轻度耦合一直是我们追求的代码风格。

坚持原创技术分享,您的支持将鼓励我继续创作!