IOC
概念
将对象的创建进行反转,由Spring框架为我们创建对象
1、基于XML配置创建对象
引入pom文件
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> </dependency>
创建user对象:
@Data public class User { private String id; private String name; }
创建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" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p" 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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd"> <bean id="user" class="com.example.demo.ioc.User"> <property name="id" value="1"></property> <property name="name" value="aaa"></property> </bean> </beans>
测试代码:
public static void main(String[] args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml"); System.out.println(applicationContext.getBean("user")); }
输出结果:
User(id=1, name=aaa, address=null)
2、基于注解:
配置类:
新建BeanConfig类:
@Configuration public class BeanConfig { @Bean public User getUser() { User user = new User(); user.setId("1"); user.setName("a"); return user; } }
测试类:
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(BeanConfig.class); System.out.println(applicationContext.getBean(User.class)); System.out.println(applicationContext.getBean("getUser")); //或者加value值通过如下方式获取:@Bean(value = "user") //System.out.println(applicationContext.getBean("user"));
输出结果:
User(id=1, name=a) User(id=1, name=a)
3、扫描包+注解
User类上加注解:
@Data @Component public class User { @Value(value = "1") private String id; @Value(value = "aa") private String name; }
测试代码:
ApplicationContext applicationContext = new AnnotationConfigApplicationContext("com.example.demo.ioc"); System.out.println(applicationContext.getBean(User.class));
输出结果:
User(id=1, name=a)
自动创建对象,完成依赖注入
User对象中建Address对象
@Data @Component public class User { @Value(value = "1") private String id; @Value(value = "aa") private String name; @Autowired private Address address; } @Data @Component class Address { @Value(value = "上海") private String province; @Value(value = "浦东") private String city; @Value(value = "康桥") private String area; }
测试代码:
ApplicationContext applicationContext = new AnnotationConfigApplicationContext("com.example.demo.ioc"); System.out.println(applicationContext.getBean(User.class));
输出结果:
User(id=1, name=aa, address=Address(province=上海, city=浦东, area=康桥))
@Autowired是类型注入,可使用@Qualifier(value = “user”)使用名称注入,子类Address要加上注解@Component(value = “user”)
AOP
面向切面编程,抽象化面向对象
使用场景:
1、打印日志
2、事务
3、权限处理
案例
实现加减法执行前后输出日志的具体实现
添加pom
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> </dependency>
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" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p" 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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd"> <!--自动扫包--> <context:component-scan base-package="com.example.demo.aop"></context:component-scan> <!--开启自动生成代理--> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> </beans>
定义接口跟实现类
@Component public interface Calc { int add(int a, int b); int sub(int a, int b); } @Component public class CalcImpl implements Calc { @Override public int add(int a, int b) { int c = a + b; return c; } @Override public int sub(int a, int b) { int c = a - b; return c; } }
定义切面类
@Aspect @Component public class LoggerAspect { @Before("execution(public int com.example.demo.aop.CalcImpl.*(..))") public void before(JoinPoint joinpoint) { String name = joinpoint.getSignature().getName(); System.out.println(name + "方法参数是" + Arrays.toString(joinpoint.getArgs())); } @AfterReturning(value = "execution(public int com.example.demo.aop.CalcImpl.*(..))", returning = "result") public void after(JoinPoint joinpoint, Object result) { String name = joinpoint.getSignature().getName(); System.out.println(name + "方法结果是" + result); } }
编写测试类
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml"); Calc calc = applicationContext.getBean(Calc.class); System.out.println(calc.add(3, 2)); System.out.println(calc.sub(3, 2));
输出结果:
add方法参数是[3, 2] add方法结果是5 5 sub方法参数是[3, 2] sub方法结果是1