Spring的bean管理(註解)
Spring註解開發準備
1 導入jar包
(1)導入基本的jar包
- commons-logging.jar
- log4j.jar
- spring-beans.RELEASE.jar
- spring-context.RELEASE.jar
- spring-core.RELEASE.jar
- spring-expression.RELEASE.jar
(2) 導入aop的jar包
- spring-aop-RELEASE.jar
2 創建類,創建方法
3 創建spring配置文件,引入約束
(1) ioc基本功能,引入約束beans
(2) 做spring的ioc註解開發,引入新的約束
<?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" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- bean definitions here --> </beans>
4 開啓註解掃描
<!-- 開啓註解掃描 (1)到包裏面掃描類.方法.屬性上面是否有註解 --> <context:component-scan base-package="com.jia"></context:component-scan> <!-- 掃描屬性上面的註解 --> <context:annotation-config></context:annotation-config>
註解創建對象
1 在創建對象的類上面使用註解實現
@Component(value="user") //<bean id="user" class=""/> public class User { }
2 創建對象有四個註解:
Spring中提供@Component的三個衍生註解:
@Component
@Controller: WEB層
- @Service: 業務層
- @Repository: 持久層
3 創建對象單實例還是多實例
@Component(value="user") //<bean id="user" class=""/> @Scope(value="prototype") //多實例 public class User { }
註解注入屬性
//創建service類,創建dao類,在service得到dao對象
@Component(value="userService")
public class UserService {
/**
* 得到dao對象
* 定義dao類型屬性
* 在dao屬性上面使用註解完成對象注入
* 使用註解方時候不需要set方法
* @Autowired
*/
@Autowired
private UserDao dao;
/**
* @Resource
* name屬性值寫註解創建dao對象value值
*/
@Resource(name="userDao")
private UserDao userDao;
}
配置文件和註解混合使用
1 創建對象操作使用配置文件方式實現
2 注入屬性的操作使用註解方式實現
AOP
AOP概念
- 1 aop:面向切面(方面)編程,擴展功能不修改源代碼實現
- 2 AOP採取橫向抽取機制,取代了傳統縱向繼承體系重複性代碼
AOP原理
動態代理
aop操作術語
- Joinpoint(連接點):類裏面可以被增強的方法,這些方法稱爲連接點
Pointcut(切入點):
在類裏面可以有很多的方法被增強,實際被增強的方法稱爲切入點
Advice(通知/增強):
增強的邏輯,稱爲增強,比如擴展日誌功能,這個日誌功能稱爲增強
- 前置通知:在方法之前執行
- 後置通知:在方法之後執行
- 異常通知:在方法出現異常
- 最終通知:在後置之後執行
- 環繞通知:在方法之前和之後執行
Aspect(切面):
把增強應用到具體方法上面,過程稱爲切面,把增強用到切入點過程
基於aspectJ的XML準備工作
Spring的aop操作:
1 在spring裏面進行aop操作,使用aspectj實現
- (1) aspectj不是spring一部分,和spring一起使用進行aop操作
- (2) Spring2.0以後新增了對AspectJ支持
2 使用aspectJ實現aop有兩種方式
- (1)基於aspectJ的Xml配置
- (2)基於aspectJ的註解方式
Aop操作準備
- 1 除了導入基本的Jar包之外,還需要導入aop相關的jar包
- aopalliance.jar
- aspectjweaver.jar
- spring-aop-RELEASE.jar
- spring-aspects-RELEASE.jar
- 2 創建spring核心配置文件,導入aop的約束
<?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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- bean definitions here --> </beans>
使用表達式配置切入點
1 切入點:實際增強的方法
2 常用的表達式
- execution(<訪問修飾符>?<返回類型><方法名>(<參數>)<異常>)
- execution(* com.jia.aop.Book.add(…))
- execution(* com.jia.aop.Book.*(…))
- execution(* * . *(…))
- 匹配所有save開頭的方法execution(* save*(..))
aspectJ的aop操作
public class MyBook { private void before1() { System.out.println("前置增強............."); } private void after1() { System.out.println("後置增強............."); } /** * 環繞增強 * * @throws Throwable */ private void around1(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { // 方法之前 System.out.println("方法之前............"); // 執行被增強的方法 proceedingJoinPoint.proceed(); // 方法之後 System.out.println("方法之後..................."); } }
<!-- 1 配置對象 --> <bean id="book" class="com.jia.aop.Book"></bean> <bean id="myBook" class="com.jia.aop.MyBook"></bean> <!-- 2 配置aop操作 --> <aop:config> <!-- 2.1 配置切入點 --> <aop:pointcut expression="execution(* com.jia.aop.Book.*(..))" id="pointcut1" /> <!-- 2.2 配置切面 把增強用到方法上面 --> <aop:aspect ref="myBook"> <!-- 配置增強類型 method: 增強類裏面使用哪個方法作爲前置 --> <aop:before method="before1" pointcut-ref="pointcut1" /> <!-- 後置增強 --> <aop:after-returning method="after1" pointcut-ref="pointcut1" /> <!-- 環繞增強 --> <aop:around method="around1" pointcut-ref="pointcut1" /> </aop:aspect> </aop:config>
Log4j介紹
1 通過log4j可以看到程序運行過程中更詳細的信息
2 使用
導入log4j的jar包
複製log4j的配置文件,複製到src下面
log4j.properties
3 設置日誌級別
log4j.rootLogger=info, stdout
- info: 看到基本信息
- debug: 看到更詳細信息
Spring整合web項目
原理:
- 在服務器啓動時候,創建對象加載配置文件
- 底層使用監聽器,ServletContext對象
在web.xml中配置Spring
封裝了一個監聽器,只需要配置監聽器就可以
配置監聽器之前:導入Spring整合web項目jar包
spring-web-RELEASE.jar
<!-- 配置Spring監聽器 --> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener>
指定加載Spring配置文件位置
<!-- 指定Spring配置文件位置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param>