PART_ONE:IOC
Spring的ioc可以解決開發(fā)過程中New對象的操作。
1.除了導(dǎo)入Spring的jar包還要導(dǎo)入之后aop需要的jar包
2.xml配置的方式
a.簡單的User.class
package entiy;
public class User {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void showUser() {
System.out.println("i am a user");
}
}
b.Spring的核心配置文件,applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
<bean id="user" class="entiy.User"></bean>
<bean id="userDao" class="dao.UserDaoImpl"></bean>
<bean id="userSerive" class="dao.UserService">
<property name="userDao" ref="userDao"></property>
</bean>
<!-- 開啟掃描注解鹅士,對類,方法泛烙,屬性上的注解進行掃描 -->
<context:component-scan base-package="entiy,dao,aop"></context:component-scan>
</beans>
c.獲取User實例
@Test
public void test() {
//加載spring配置文件
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
User test = (User) context.getBean("user");
test.showUser();
}
d.bean方式配置的時候锨推,要注意User類中是否有無參構(gòu)造函數(shù)
3.ioc的注解方式實現(xiàn)
a.在applicationContext.xml中加入注釋的支持
<bean id="userDao" class="dao.UserDaoImpl"></bean>
<bean id="userSerive" class="dao.UserService">
<property name="userDao" ref="userDao"></property>
</bean>
<!-- 開啟掃描注解,對類介陶,方法,屬性上的注解進行掃描 -->
<context:component-scan base-package="entiy,dao,aop"></context:component-scan>
<context:component-scan base-package="cn.itcast.aop"></context:component-scan>
b.測試類
dao
public interface PersonDao {
public void showName(Person person);
}
daoimp
package dao;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import entiy.Person;
@Repository
public class PersonDaoImpl implements PersonDao{
@Override
public void showName(Person person) {
System.out.println(person.getName());
}
}
service
package dao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import entiy.Person;
@Service(value="personService")
public class PersonService {
@Autowired
private PersonDao personDao;
public void showName(Person person) {
personDao.showName(person);
}
}
test
@Test
public void testService() {
//加載spring配置文件
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService service = (UserService) context.getBean("userSerive");
User test = new User();
test.setName("handsomeboy");
service.showName(test);
}
c.四個注解的簡單介紹
PART_TWO:AOP操作
AOP的四個概念
Joinpoint(連接點): 類里面可以被增強的方法色建,這些方法稱為連接點
Pointcut(切入點):所謂切入點是指我們要對哪些Joinpoint進行攔截的定義.
Advice(通知/增強):所謂通知是指攔截到Joinpoint之后所要做的事情就是通知.通知分為前置通知,后置通知,異常通知,最終通知,環(huán)繞通知(切面要完成的功能)
Aspect(切面): 是切入點和通知(引介)的結(jié)合
Introduction(引介):引介是一種特殊的通知在不修改類代碼的前提下, Introduction可以在運行期為類動態(tài)地添加一些方法或Field.
Target(目標(biāo)對象):代理的目標(biāo)對象(要增強的類)
Weaving(織入):是把增強應(yīng)用到目標(biāo)的過程.
把advice 應(yīng)用到 target的過程
Proxy(代理):一個類被AOP織入增強后哺呜,就產(chǎn)生一個結(jié)果代理類
1.xml配置方式實現(xiàn)
a.Book.class
public class Book {
public void showBook() {
System.out.println("this is a book");
}
}
b.MyBook.class加在book的方法類
public class MyBook {
public void before() {
System.out.println("this is before");
}
}
c.在applicationContext.xml加入aop配置
<!-- 配置aop操作 -->
<bean id="book" class="aop.Book"></bean>
<bean id="myBook" class="aop.MyBook"></bean>
<aop:config>
<!-- 配置切入點 -->
<aop:pointcut expression="execution(* aop.Book.showBook(..))" id="bookPoint"/>
<!-- 配置切面 -->
<aop:aspect ref="myBook">
<aop:before method="before" pointcut-ref="bookPoint"/>
</aop:aspect>
</aop:config> -->
d.測試
@Test
public void test() {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Book book = (Book) context.getBean("book");
book.showBook();
}
2.注解方式實現(xiàn)
a.在applicationContext.xml加入aop注解
<!-- 開啟對 @Aspect 的支持-->
<aop:aspectj-autoproxy/>
b.myBook.class
@Aspect
public class MyBook2 {
@Pointcut("execution(* aop.Book2.showBook(..))")
public void showBook() {}
@Before("showBook")
public void before() {
System.out.println("this is before2");
}
}