1、依赖
<!-- https://mvnrepository.com/artifact/org.dom4j/dom4j -->
<dependency>
<groupId>org.dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>2.1.3</version>
</dependency>
XPath
<!-- https://mvnrepository.com/artifact/jaxen/jaxen -->
<dependency>
<groupId>jaxen</groupId>
<artifactId>jaxen</artifactId>
<version>2.0.0</version>
</dependency>
2、spring依赖
spring.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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="..." class="..."> (1) (2)
<!-- collaborators and configuration for this bean go here -->
</bean>
<bean id="..." class="...">
<!-- collaborators and configuration for this bean go here -->
</bean>
<!-- more bean definitions go here -->
</beans>
3、@Resource注解
spring.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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
</beans>
https://mvnrepository.com/artifact/javax.annotation/javax.annotation-api
<!-- https://mvnrepository.com/artifact/javax.annotation/javax.annotation-api -->
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
4、@Autowired自动化注解
在一个类中注入一个bean对象,@Autowired提示Autowired members must be defined in valid Spring bean (@Component|@Service|…),它的含义就是:自动注入对象必须定义在有效的spring bean内,也就是说只有本身作为bean的类才能注入其他对象。
5、Spring IOC扫描器
package com.yjh.dao;
import org.springframework.stereotype.Repository;
@Repository
public class GoodsDao {
public void test(){
System.out.println("GoodsDao Test...");
}
}
package com.yjh.service;
import com.yjh.dao.GoodsDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class GoodsService {
@Autowired
private GoodsDao goodsDao;
public void test(){
System.out.println("GoodsService Test...");
goodsDao.test();
}
}
package com.yjh.controller;
import com.yjh.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
@Controller
public class GoodsController {
@Autowired
private GoodsService goodsService;
public void test(){
System.out.println("GoodsController Test...");
goodsService.test();
}
}
springIOC.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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!--
Spring IOC扫描器
作用:bean对象的统一管理,简化开发配置,提高开发效率
1、设置自动化扫描的范围
如果bean对象未在扫描范围,即使声明了注解,也不会被实例化
2、在需要被实例化的javabean的类上添加指定的注解(注解声明在类级别)(bean对象的id默认值是类的首字母小写)
Dao层:@Repository
Service层:@Service
Controller层:@Controller
任意类:@Component
-->
<context:component-scan base-package="com.yjh"/>
</beans>
package com.yjh;
import com.yjh.controller.GoodsController;
import com.yjh.service.GoodsService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App03 {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("SpringIOC.xml");
// GoodsDao goodsDao = (GoodsDao) ac.getBean("goodsDao");
// goodsDao.test();
// GoodsService goodsService = (GoodsService) ac.getBean("goodsService");
// goodsService.test();
GoodsController goodsController = (GoodsController) ac.getBean("goodsController");
goodsController.test();
}
}
6、AOP
1、静态代理
代理的三要素(结婚)
- 有共同的行为(结婚) -定义接口
- 目标角色/真实角色(新人) -实现接口
- 代理角色(婚庆公司) -实现接口 增强用户行为
静态代理的特点
- 目标角色固定
- 在应用程序之前就得知目标角色
- 代理对象会增强目标对象的行为
- 也可能存在多个代理,产生“类爆炸”(缺点)
结婚为例:
package com.yjh.proxy;
/**
* 定义接口 ->定义行为
*/
public interface Marry {
public void toMarry();
}
package com.yjh.proxy;
/**
* 静态代理 -> 目标角色(真实角色)
* 实现行为
*/
public class You implements Marry{
@Override
public void toMarry() {
System.out.println("我要结婚了...");
}
}
package com.yjh.proxy;
/**
* 静态代理 -> 代理角色
* 实现行为
* 增强用户行为
*/
public class MarryCompanyProxy implements Marry{
//目标对象
private Marry target;
//通过带参构造器传递目标对象
public MarryCompanyProxy(Marry target){
this.target = target;
}
@Override
public void toMarry() {
//用户行为增强
before();
//调用目标对象中的方法
target.toMarry();
//用户行为增强
after();
}
private void after() {
System.out.println("新年快乐!百年好合!");
}
private void before() {
System.out.println("婚礼现场正在布置中...");
}
}
package com.yjh.proxy;
public class StaticProxy {
public static void main(String[] args) {
//目标对象
You you = new You();
//代理对象
MarryCompanyProxy mc = new MarryCompanyProxy(you);
//通过代理对象对调用目标对象中的方法
mc.toMarry();
//目标对象
//Owner o = new Owner();
//代理对象
//AgentProxy ap = new AgentProxy(o);
//通过代理对象对调用目标对象中的方法
//ap.toRentHouse();
}
}