Spring3注解零配置

咱们在之前学习 Spring 的时候,其全部的配置信息都写在 applicationContext.xml 里,大体示例以下: 

java代码: 
查看复制到剪贴板打印 
<beans> 
<bean name="ds" class="org.apache.commons.dbcp.BasicDataSource"> 
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/> 
<property name="url" value="jdbc:oracle:thin:@localhost :1521:wangbin"/> 
<property name="username" value="tech37"/> 
<property name="password" value="tech37"/> 
</bean> 
  
<bean name="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
<property name="dataSource" ref="ds"/> 
</bean> 
<tx:advice id="txAdvice" transaction-manager="txManager"> 
<tx:attributes> 
<tx:method name="get*" read-only="true"/> 
<tx:method name="*"/> 
</tx:attributes> 
</tx:advice> 
  
<aop:config> 
<aop:advisor advice-ref="txAdvice" 
pointcut="execution(* cn.javass..business.ebo.*Ebo.*(..))"/> 
</aop:config> 
</beans> 


在上面的示例中,咱们能够典型的看到Spring的三种功能: 
一、IoC容器,如: 
<bean …> 
<property…/> 
</bean> 
二、AOP 
<aop:config/> 
三、事务 
<tx:advice/> 
首先咱们学习如何使用注解来构造IoC容器。 
用注解来向Spring容器注册Bean。须要在applicationContext.xml中注册<context:component-scan base-package=“cn.javass”/>。代表cn.javass包及其子包中,若是某个类的头上带有特定的注解【@Component/@Repository/@Service/@Controller】,就会将这个对象做为Bean注册进Spring容器。 
以上的4个注解,用法彻底一摸同样,只有语义上的区别。 
@Component 是全部受Spring 管理组件的通用形式,Spring 还提供了更加细化的注解形式:  @Repository 、@Service 、@Controller ,它们分别对应数据层Bean ,业务层Bean ,和表现层Bean 。 
其中,@Component不推荐使用。 
这四个注解均可以放在类的头上,若是不指定其value【@Service】,则默认的bean名字为这个类的简单类名首字母小写;若是指定value【@Service(“dao”)】,则使用value做为ban名字。 
注意:若是cn.javass.SampleDao和cn.javass1.SampleDao都只用@Service注解,而不指定value会发生什么事? 
除了注册Bean以外,还能够经过在<bean>上设置属性来控制Bean的其余属性,好比: 
<bean name="" class="" 
lazy-init=“true”  //是否延迟初始化 
scope=“prototype”  //bean的生命周期 
depends-on=“其余bean“ //依赖其余bean 
/> 
在Spring中也有相应的注解去对应 
@Lazy 
@Scope 
@DependsOn 
他们都是放在类的头上。 
除了注册Bean以外,还能够经过在<bean>上设置属性来控制Bean的初始化方法和析构方法,好比: 
<bean name="" class="" 
init-method=“init“ //初始化方法 
destroy-method=“close“ //析构方法 
/> 
在Spring中也有相应的Bean去对应,固然,这两个注解是jdk里内置的 
@PostConstruct 
@PreDestroy 
这两个注解放在方法头上。 
@Autowired 根据bean 类型从spring 上下文中进行查找。咱们须要从如下几方面来学习这个注解: 
一、它都能放在什么头上? 
它能够放在属性、方法和构造方法头上。 
二、根据什么注入? 
2.一、若是某个接口的实现类在Spring容器中惟一的话,仅使用@Autowired就能够正确注入,如: 
@Autowired 
private SampleDao dao; 
2.二、若是某个接口的实现类在Spring容器中不惟一 
2.2.一、用@Qualifier来指定注入Bean的名字,如 
@Autowired 
@Qualifier(“sampleDaoImpl”) 
private SampleDao dao; 
2.2.二、用@Primary在多个Bean之中指定哪一个为最优先者,注意它不是跟@Autowired配合使用,而是跟@Service配合使用,如 
@Service @Primary SampleDaoImpl 
2.2.三、用属性名、方法参数名、构造方法参数名来设置注入哪一个 Bean,如 
public class SampleDaoImpl implements SampleDao 
public class SampleDaoImpl1 implements SampleDao 
对应 
@Autowired 
private SampleDao sampleDaoImpl; 
注意:属性名在编译后是必定存在的;可是方法参数名和构造方法参数名必须指定相应的编译选项才能保留。 
三、@Autowired只有一个选项, boolean required() default true;是否必须,且默认为true。所以,若是仅仅使用@Autowired,就必需要能注入,不然会报错。 

@Resource拥有和@Autowired相似的做用。 
Spring还支持使用@Configuration,把一个类做为一个IoC容器,它的某个方法头上若是注册了@Bean ,就会做为这个Spring容器中的Bean。 

java代码: 
查看复制到剪贴板打印 
@Configuration("ctx") 
public class JavaApplicationContext { 
@Bean 
public String hello(){ 
return "hello"; 

@Bean 
public int max(){ 
return 9; 


使用AnnotationConfigApplicationContext得到Spring容器 
ApplicationContext context = new AnnotationConfigApplicationContext(JavaApplicationContext.class); 
使用@ImportResource (“classpath:applicationContext.xml”)能够把其余容器导入到这个容器中。 
Spring使用的AOP注解分为三个层次: 
一、@Aspect放在类头上,把这个类做为一个切面,可是这个类必定要显式的注册在Spring容器中。 
二、 @Pointcut放在方法头上,定义一个可被别的方法引用的切入点表达式。 
三、5种通知。 www.2cto.com 
3.一、@Before,前置通知,放在方法头上。 
3.二、@After ,后置【finally】通知,放在方法头上。 
3.三、@AfterReturning,后置【try】通知,放在方法头上,使用returning来引用方法返回值。 
3.四、@AfterThrowing,后置【catch】通知,放在方法头上,使用throwing来引用抛出的异常。 
3.五、@Around,环绕通知,放在方法头上,这个方法要决定真实的方法是否执行,并且必须有返回值。 
[size=medium]
[/size]
相关文章
相关标签/搜索