Spring配置元數(shù)據(jù)3種方式
基于xml的配置文件
-
基于注解的配置
例如 通過
<context:component-scan base-package="com.company.">
spring自動掃描帶有@Component锯厢、@Repository黎烈、@Service、@Controller
標(biāo)簽的類自動注冊到spring容器.以及對標(biāo)記了@Required欢摄、@Autowired趣些、JSR250's @PostConstruct瓷耙、@PreDestroy太伊、@Resource剿另、JAX-WS's @WebServiceRef箫锤、EJB3's @EJB、JPA's @PersistenceContext雨女、@PersistenceUnit
等注解的類進(jìn)行對應(yīng)的操作使注解生效(包含了annotation-config標(biāo)簽的作用)谚攒。getBean()
的默認(rèn)名稱是類名(頭字母小寫),如果想自定義氛堕,可以@Service("aaaaa")
這樣來指定馏臭。
這種bean默認(rèn)是"singleton"
的,如果想改變讼稚,可以使用@Scope("prototype")
來改變,以及自定義初始化和銷毀方法等.@PostConstruct public void init() {...} @PreDestroy public void destory() {...}
使用
@Autowired
自動注解接口的時(shí)候,如果該接口有多個(gè) 實(shí)現(xiàn),需要@Qualifier
指定注入哪個(gè)實(shí)現(xiàn)類 基于java的配置
@Autowired用法和流程
@Autowired, 可以用在以下三個(gè)地方
- 屬性
如果用在屬性上, 可以不用謝setter
方法. - setter方法
如果用在setter
方法上,可以不用寫手動set
,會自動注入 - 構(gòu)造函數(shù)
如果用在構(gòu)造函數(shù)上, 可以不用在xml文件中配置bean的constructor-arg
的參數(shù).
@Autowired是如何自動注入的??
首先根據(jù)配置文件的bean
的id, 如果id找不到就找name
, 繼續(xù)找不到就找類型
,
即如下順序:
當(dāng)根據(jù)Type
進(jìn)行自動注入時(shí), 如果有多個(gè)相同Type
(比如一個(gè)接口有多個(gè)實(shí)現(xiàn)), 就需要@Qualifier
這個(gè)注解了, Qualifier
里面可以寫id
或者name
.
如果根據(jù)id
或者name
注入時(shí),參數(shù)/屬性的變量名必須和id/name
完全一致.
比如:
private Hello hello;
<bean id="hello" clas="com.company.HelloWorld"/>
<bean id="hello2" clas="com.company.HelloChina"/>
bean的作用域
作用域 | 描述 |
---|---|
singleton | 該作用域?qū)?bean 的定義的限制在每一個(gè) Spring IoC 容器中的一個(gè)單一實(shí)例(默認(rèn))括儒。 |
prototype | 該作用域?qū)我?bean 的定義限制在任意數(shù)量的對象實(shí)例。 |
request | 該作用域?qū)?bean 的定義限制為 HTTP 請求锐想。只在 web-aware Spring ApplicationContext 的上下文中有效帮寻。 |
session | 該作用域?qū)?bean 的定義限制為 HTTP 會話。 只在web-aware Spring ApplicationContext的上下文中有效赠摇。 |
global-session | 該作用域?qū)?bean 的定義限制為全局 HTTP 會話固逗。只在 web-aware Spring ApplicationContext 的上下文中有效 |
當(dāng)使用scope="singleton"
的時(shí)候,每次getbeean()調(diào)用的為同樣一個(gè)實(shí)例, 使用scope="prototype"
時(shí),每次都是一個(gè)新的實(shí)例
初始化回調(diào)與銷毀回調(diào)
java實(shí)現(xiàn)
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.DisposableBean;
...
public class ExampleBean implements InitializingBean,
DisposableBean{
public void afterPropertiesSet() {
// do some initialization work
}
public void destroy() {
// do some destruction work
}
...
}
或者基于xml配置
<bean id="exampleBean"
class="examples.ExampleBean"
init-method="init"
destroy-method="destroy"/>
...
public class ExampleBean {
public void init() {
// do some initialization work
}
public void destroy() {
// do some destruction work
}
}
建議不要使用 InitializingBean 或者 DisposableBean 的回調(diào)方法浅蚪,因?yàn)?XML 配置在命名方法上提供了極大
的靈活性
提供所有bean默認(rèn)的初始化和銷毀方法
如果你有太多具有相同名稱的初始化或者銷毀方法的 Bean,那么你不需要在每一個(gè) bean 上聲明 初始化方法和
銷毀方法烫罩【虮桑框架使用 元素中的 d default-init-method 和 d default-destroy-method 屬性提供了靈活地配置這種
情況,如下所示:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
default-init-method="init"
default-destroy-method="destroy">
<bean id="..." class="...">
<!-- collaborators and configuration for this bean go here -->
</bean>
</beans>
注入集合
list, set, map, prorps
<property name="list">
<list>
<ref bean="helloChina"/>
<value>hello</value>
<value>hi</value>
</list>
</property>
<property name="map">
<map>
<entry key="1" value="INDIA"/>
<entry key="2" value="USA"/>
</map>
</property>
<property name="set">
<set>
<value>INDIA</value>
<value>USA</value>
</set>
</property>
p-namespace簡化xml配置
例子:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="john-classic" class="com.example.Person">
<property name="name" value="John Doe"/>
<property name="spouse" ref="jane"/>
</bean>
<bean name="jane" class="com.example.Person">
<property name="name" value="John Doe"/>
</bean>
</beans>
------------------
<?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" <!--要添加這條配置-->
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="john-classic" class="com.example.Person"
p:name="John Doe"
p:spouse-ref="jane"/> <!-- -ref部分表明引用 -->
</bean>
<bean name="jane" class="com.example.Person"
p:name="John Doe"/>
</bean>
</beans>
Spring-AOP
aop命名空間標(biāo)簽
<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
<!-- bean definition & AOP specific configuration -->
</beans>
配置依賴
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.9</version>
</dependency>
<!-- https://mvnrepository.com/artifact/aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.9</version>
</dependency>
聲明一個(gè)切入點(diǎn)
- 基于xml配置
<aop:config>
<aop:aspect id="myAspect" ref="aBean">
<aop:pointcut id="businessService"
expression="execution(* com.xyz.myapp.service.*.*(..))"/>
<!-- a before advice definition -->
<aop:before pointcut-ref="businessService" method="doRequiredTask"/>
<!-- an after advice definition -->
<aop:after pointcut-ref="businessService" method="doRequiredTask"/>
<!-- an after-returning advice definition -->
<!--The doRequiredTask method must have parameter named retVal -->
<aop:after-returning pointcut-ref="businessService" returning="retVal" method="doRequiredTask"/>
<!-- an after-throwing advice definition -->
<!--The doRequiredTask method must have parameter named ex -->
<aop:after-throwing pointcut-ref="businessService" throwing="ex" method="doRequiredTask"/>
<!-- an around advice definition -->
<aop:around pointcut-ref="businessService" method="doRequiredTask"/>
...
</aop:aspect>
</aop:config>
<bean id="aBean" class="...">
...
</bean>
(..)
表示方法可能有參數(shù).
- 通過注解配置
xml中添加<aop:aspectj-autoproxy/>
配置實(shí)例:
@Aspect
public class AspectModule {
@Pointcut("execution(* com.hand.aop.Hello.test())")
private void testService(){}
@Before("testService()")
public void doBeforeTask() {
System.out.println("before task");
}
@After("testService()")
public void doAfterTask(){
System.out.println("after task");
}
@AfterReturning(pointcut = "testService()", returning="retVal")
public void doAfterReturnningTask(Object retVal){
System.out.println("doAfterReturnningTask");
}
@AfterThrowing(pointcut = "testService()", throwing="ex")
public void doAfterThrowingTask(Exception ex){
System.out.println("doAfterThrowingTask");
}
@Around("testService()")
public void doAroundTask() {
System.out.println("doAroundTask");
}
}
---
<bean id="aspectModule" class="com.company.AspectModule"/>