Spring3注解零配置 .
我們在以前學習Spring的時候,其所有的配置信息都寫在applicationContext.xml里,大致示例如下:
java代碼:
- <beans>
- <beanname="ds"class="org.apache.commons.dbcp.BasicDataSource">
- <propertyname="driverClassName"value="oracle.jdbc.driver.OracleDriver"/>
- <propertyname="url"value="jdbc:oracle:thin:@localhost:1521:wangbin"/>
- <propertyname="username"value="tech37"/>
- <propertyname="password"value="tech37"/>
- </bean>
- <beanname="txManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <propertyname="dataSource"ref="ds"/>
- </bean>
- <tx:adviceid="txAdvice"transaction-manager="txManager">
- <tx:attributes>
- <tx:methodname="get*"read-only="true"/>
- <tx:methodname="*"/>
- </tx:attributes>
- </tx:advice>
- <aop:config>
- <aop:advisoradvice-ref="txAdvice"
- pointcut="execution(*cn.javass..business.ebo.*Ebo.*(..))"/>
- </aop:config>
- </beans>
在上面的示例中,我們可以典型的看到Spring的三種功能:
1、IoC容器,如:
<bean …>
<property…/>
</bean>
2、AOP
<aop:config/>
3、事務
<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 上下文中進行查找。我們需要從以下幾方面來學習這個注解:
1、它都能放在什么頭上?
它可以放在屬性、方法和構造方法頭上。
2、根據什么注入?
2.1、如果某個接口的實現類在Spring容器中唯一的話,tb僅使用@Autowired就可以正確注入,如:
@Autowired
private SampleDao dao;
2.2、如果某個接口的實現類在Spring容器中不唯一
2.2.1、用@Qualifier來指定注入Bean的名字,如
@Autowired
@Qualifier(“sampleDaoImpl”)
private SampleDao dao;
2.2.2、用@Primary在多個Bean之中指定哪個為最優先者,注意它不是跟@Autowired配合使用,而是跟@Service配合使用,如
@Service @Primary SampleDaoImpl
2.2.3、用屬性名、方法參數名、構造方法參數名來設置注入哪個 Bean,如
public class SampleDaoImpl implements SampleDao
public class SampleDaoImpl1 implements SampleDao
對應
@Autowired
private SampleDao sampleDaoImpl;
注意:屬性名在編譯后是一定存在的;但是方法參數名和構造方法參數名必須指定相應的編譯選項才能保留。
3、@Autowired只有一個選項, boolean required() default true;是否必須,且默認為true。因此,如果僅僅使用@Autowired,就必須要能注入,否則會報錯。
@Resource擁有和@Autowired類似的作用。
Spring還支持使用@Configuration,把一個類作為一個IoC容器,它的某個方法頭上如果注冊了@Bean,就會作為這個Spring容器中的Bean。
使用AnnotationConfigApplicationContext獲tb得Spring容器
ApplicationContext context = new AnnotationConfigApplicationContext(JavaApplicationContext.class);
使用@ImportResource (“classpath:applicationContext.xml”)可以把其他容器導入到這個容器中。
Spring使用的AOP注解分為三個層次:
1、@Aspect放在類頭上,把這個類作為一個切面,但是這個類一定要顯式的注冊在Spring容器中。
2、 @Pointcut放在方法頭上,定義一個可被別的方法引用的切入點表達式。
3、5種通知。
3.1、@Before,前置通知,放在方法頭上。
3.2、@After,后置【finally】通知,放在方法頭上。
3.3、@AfterReturning,后置【try】通知,放在方法頭上,使用returning來引用方法返回值。
3.4、@AfterThrowing,后置【catch】通知,放在方法頭上,使用throwing來引用拋出的異常。
3.5、@Around,環繞通知,放在方法頭上,這個方法要決定真實的方法是否執行,而且必須有返回值。