使用注解可以减少代码的开发量,同时spring也提供了丰富的注解功能。小编将介绍了一下一些常用的注解同时在文末展示spring4的注解。不当之处,希望大家批评指导哦。
·spring支持的注解方式
1、bean
<bean class="org.springframework.beans.factory.annotation. AutowiredAnnotationBeanPostProcessor"/>
<context:annotationconfig /> 将隐式地向 Spring 容器注册 AutowiredAnnotationBeanPostProcessor、
CommonAnnotationBeanPostProcessor 、 PersistenceAnnotationBeanPostProcessor 以及
RequiredAnnotationBeanPostProcessor 这 4 个 BeanPostProcessor 。
3、命名空间<context:component-scan/>
<!-- 自动扫描与装配bean --> <context:component-scan base-package="cn.itcast.oa"></context:component-scan>
---类级别的常用注解:
@Controller:标注一个控制器组件类
@Service:标注一个业务逻辑组件类
@Repository:标注一个DAO组件类
@Component:标注一个普通的spring Bean类
@Scope:指定Bean实例的作用域
---类内部的常用注解:
@Autowire:对类成员变量、方法及构造函数进行标注,完成自动装配工作。
@Resource:作用类似Autowire,使用时需要引入common-annotations.jar
@Value等等。
·最近做过的一个小实例:
最开始引入相关jar。
1、spring测试类
@Controller @Scope("prototype") public class TestAction extends ActionSupport { @Resource private TestService testService; @Override public String execute() throws Exception { System.out.println("---> TestAction.execute()"); testService.saveTwoUsers(); return "success"; } }
public class SpringTest { private ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); @Test public void testBean() throws Exception { TestAction testAction = (TestAction) ac.getBean("testAction"); System.out.println(testAction); } // 测试SessionFactory @Test public void testSessionFactory() throws Exception { SessionFactory sessionFactory = (SessionFactory) ac.getBean("sessionFactory"); System.out.println(sessionFactory); } // 测试事务 @Test public void testTransaction() throws Exception { TestService testService = (TestService) ac.getBean("testService"); testService.saveTwoUsers(); } }
<?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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- 自动扫描与装配bean --> <context:component-scan base-package="cn.itcast.oa"></context:component-scan> <!-- 导入外部的properties文件 --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 配置SessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <!-- 指定hibernate的配置文件位置 --> <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> <!-- 配置c3p0数据库连接池 --> <property name="dataSource"> <bean class="com.mchange.v2.c3p0.ComboPooledDataSource"> <!-- 数据连接信息 --> <property name="jdbcUrl" value="${jdbcUrl}"></property> <property name="driverClass" value="${driverClass}"></property> <property name="user" value="${user}"></property> <property name="password" value="${password}"></property> <!-- 其他配置 --> <!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 --> <property name="initialPoolSize" value="3"></property> <!--连接池中保留的最小连接数。Default: 3 --> <property name="minPoolSize" value="3"></property> <!--连接池中保留的最大连接数。Default: 15 --> <property name="maxPoolSize" value="5"></property> <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 --> <property name="acquireIncrement" value="3"></property> <!-- 控制数据源内加载的PreparedStatements数量。如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0 --> <property name="maxStatements" value="8"></property> <!--maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default: 0 --> <property name="maxStatementsPerConnection" value="5"></property> <!--最大空闲时间,1800秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 --> <property name="maxIdleTime" value="1800"></property> </bean> </property> </bean> <!-- 配置声明式事务管理(采用注解的方式) --> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <tx:annotation-driven transaction-manager="txManager"/> </beans>
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- 配置Spring的用于初始化容器对象的监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext*.xml</param-value> </context-param> <!-- 用于做初始化工作的监听器,一定要配置到Spring的ContextLoaderListener之后,因为要用到Spring的容器对象 --> <listener> <listener-class>cn.itcast.oa.util.InitListener</listener-class> </listener> <!-- 配置Spring的用于解决懒加载问题的过滤器 --> <filter> <filter-name>OpenSessionInViewFilter</filter-name> <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>OpenSessionInViewFilter</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
·学习小结
注解本身不做任何事情,只是像XML文件一样起到配置作用。
注解代表的是某种业务意义,首先解析所有属性,判断属性上是否存在指定注解,如果存在则根据搜索规则取得bean,然后利用反射原理注入,如果标注在字段上面,也可以通过字段的反射技术取得注解,根据搜索规则取得bean,然后利用反射技术注入。