一,概念:
Spring是一個基於IOC和AOP的結構J2EE系統的框架 IOC 反轉控制 是Spring的基礎,Inversion Of Control
簡單說就是創建對象由以前的程序員自己new 構造方法來調用,變成了交由Spring創建對象
DI 依賴注入 Dependency Inject. 簡單地說就是拿到的對象的屬性,已經被注入好相關值了,直接使用即可。
二,配置
2.註解
applicationContext.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" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="cn.sleepq123.pojo"></context:component-scan>
<context:component-scan base-package="cn.sleepq123.service"/>
<context:component-scan base-package="cn.sleepq123.aspect"/>
<aop:aspectj-autoproxy/> <!--開啓aop支持-->
</beans>
cn.sleepq123.pojo (放javaBean的包)
Product.java
@Component("p")
public class Product {
private int id = 1;
private String name ="小米";
@Autowired
Category category;
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@Component 對bean類的註解配置
@Autowired 對象注入
注意:必須在applicationContext.xml下添加下列代碼
context:component-scan base-package="cn.sleepq123.pojo"></context:component-scan>
個人理解:這個就像掃描器
三,使用
ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"applicationContext.xml"});
Product product = (ProductService) context.getBean("p");
System.out.println(product.getName());
--------------------------------------------------分界線-----------------------------------------------------
一,AOP
AOP 即 Aspect Oriented Program 面向切面編程
首先,在面向切面編程的思想裏面,把功能分爲核心業務功能,和周邊功能。
所謂的核心業務,比如登陸,增加數據,刪除數據都叫核心業務
所謂的周邊功能,比如性能統計,日誌,事務管理等等
周邊功能在Spring的面向切面編程AOP思想裏,即被定義爲切面在面向切面編程AOP的思想裏面,核心業務功能和切面功能分別獨立進行開發
然後把切面功能和核心業務功能 "編織" 在一起,這就叫AOP
1.applicationContext.xml配置
<context:component-scan base-package="cn.sleepq123.pojo"></context:component-scan>
<context:component-scan base-package="cn.sleepq123.service"/>
<context:component-scan base-package="cn.sleepq123.aspect"/>
<aop:aspectj-autoproxy/>
2.ProductService.java 服務業務(核心業務)
@Component("s")
public class ProductService {
public void doService() {
System.out.println("dododo");
}
}
3.LoggerAspect.java 日誌業務(周邊功能)
@Aspect
@Component
public class LoggerAspect {
@Around(value = "execution(* cn.sleepq123.service.ProductService.*(..))")
public Object log(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("start log:" + joinPoint.getSignature().getName());
Object object = joinPoint.proceed();
System.out.println("end log:" + joinPoint.getSignature().getName());
return object;
}
}
@Aspect 這是切面@Component 表示這是一個bean,由Spring進行管理
@Around(value = "execution(* cm.sleepq123.service.ProductService.*(..))") 表示對cn.sleepq123.service.ProductService 這個類中的所有方法進行切面操作
4.test.java 測試
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"applicationContext.xml"});
ProductService service = (ProductService) context.getBean("s");
service.doService();
}
}
打印結果
start log:doService
dododo
end log:doService
------------------------------------------------分界線-------------------------------------------------------------
使用Junit
1.導包
需要Junit 和 hamcrest-all 倆個jar包
2.SpringTest.java
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class SpringTest {
@Autowired
Category category;
@org.junit.Test
public void test() {
System.out.println(category.getName());
}
}
@RunWith 指定是spring的測試類
@ContextConfiguration 指定spring的配置文件
@Test 測試邏輯
--END--