Spring
一 . IoC
控制反转,将对象的创建进行反转,常规情况下,对象都是开发者手动创建的,使用IoC开发者不再需要创建对象,而是由IoC容器根据需求自动创建所需要的对象
不用IoC:所有对象由开发者自己创建
使用IoC:对象不用开发者创建,而是交给Spring框架来完成
环境配置 pom.xml
<dependency> <!--IoC环境引入--><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.1.9.RELEASE</version></dependency><dependency> <!--lombok 自动生成set get方法以及构造器 java类中@Data调用--><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.8</version><scope>provided</scope></dependency>
用IoC创建对象 通过spring.xml来管理创建对象 需要定义实体类
1.XML文件
基于XML:开发者把需要创建的对象在XML中进行配置,Spring框架读取这个配置文件,根据配置文件的内容来创建对象
spring.xml
<bean id="student" class="wwjDemo.Student"><property name="id" value="1"></property><property name="age" value="10"></property><property name="name" value="傲世宗师"></property><property name="addresses"><list><ref bean="address"></ref><ref bean="address2"></ref></list></property></bean>
Student类
@Datapublic class Student {private long id;private String name;private int age;private List<Address> addresses;}
main函数
加载配置文件 调用bean
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");//读取XML配置IoC容器Student student=(Student) applicationContext.getBean("student1");//获取XML中的 BeanSystem.out.println(sucker);
输出界面
2.注解方法
配置文件 BeanConfiguration
用一个java类来代替XML文件,把在XML中配置的内容放到配置类中,在Java类中添加@Configuration注解
@Configuration //public class BeanConfiguration {@Bean(value = "翼豹")//提供一个Id 等同于 name="翼豹"public Car car(){Car car = new Car();car.setId(1L);car.setName("SUBARU WRX STI");return car;}}
实体类Car
@Data//实体类public class Car {private long id;private String name;}
调用IoC中的bean使用context.getBean方法
ApplicationContext context = new AnnotationConfigApplicationContext(BeanConfiguration.class);//配置文件System.out.println(context.getBean("翼豹");
与调用XML文件配置的不同之处在于AnnotationConfigApplicationContext();
ApplicationContext context = new AnnotationConfigApplicationContext(.class)
输出界面
3.扫包+注解
更简单的方式 ,不再需要依赖于XML文件或者配置类,而是直接将bean的创建交给目标类,在目标类添加注解来创建,通过添加@Component来告知spring框架,当前类是需要注入IoC的
“注解”
@Component告知spring框架当前类需要注入IoC
@Value() 赋值
package wwj.apex.ioc;import lombok.Data;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Component;@Data@Component("fxxk")public class DataConfig {@Value("localhost:3306")private String url;@Value("Driver")private String driverName;@Value("wwj")private String username;@Value("123456")private String password;}
“扫包”
注入IoC容器后开始扫包,依次遍历所选包中的类,有关键注解@Component的包就注入IoC AnnotationConfigApplicationContext(“wwj.apex.ioc”);
public class test4 {public static void main(String[] args) {ApplicationContext context =new AnnotationConfigApplicationContext("wwj.apex.ioc");System.out.println(context.getBean(DataConfig.class));}}
自动装载
@Autowired通过类型(byType)进行注入,
如果需要通过名称(byName)取值需要使用@Qualifier(“xx”) ,完成名称的映射
@Component@Datapublic class GlobalConfig {@Value("8080")private String port;@Value("/")private String path;@Autowired //自动装载@Qualifier("fxxk")private DataConfig sb;}
二. AOP
面向切面编程,是一种抽象化的面向对象编程,对面向对象编程的一种补充
/打印日志:业务代码和打印日志耦合
计算机方法中,日志和业务代码混合在一起,AOP就是将二者解耦合,将日志代码抽象出来,统一进行处理,计算机房中只保留核心业务代码.
做到核心业务和非业务代码的解耦合
1.创建切面类
方法执行之前使用
@Before(“execution(public int wwj.apex.aop.CalImpl.*(..))”)
方法执行完成之后使用
@AfterReturning(value = “execution(public int wwj.apex.aop.CalImpl.*(..))”,returning = “result”)
@Component//@Aspectpublic class LoggerAspect {@Before("execution(public int wwj.apex.aop.CalImpl.*(..))") //方法执行之前public void before(JoinPoint joinPoint){String name = joinPoint.getSignature().getName();System.out.println(name+"add方法的参数是"+ Arrays.toString(joinPoint.getArgs()));}@AfterReturning(value = "execution(public int wwj.apex.aop.CalImpl.*(..))",returning = "result")//方法执行完成之后 , 拿不到返回值public void afterReturning(JoinPoint joinPoint,Object result){String name =joinPoint.getSignature().getName();System.out.println(name + "方法的结果是"+result);}}
@Component/public class CalImpl implements Cal {public int add(int num1, int num2) {int result = num1 + num2;return result;}public int sub(int num1, int num2) {int result = num1 - num2;return result;}public int mul(int num1, int num2) {int result = num1 * num2;return result;}public int div(int num1, int num2) {int result = num1 / num2;return result;}}
public interface Cal {public int add(int num1,int num2);public int sub(int num1,int num2);public int mul(int num1,int num2);public int div(int num1,int num2);}
2.配置自动扫包,开启自动生成代理对象
springaop.xml
<!-- 自动扫包--><context:component-scan base-package="wwj.apex.aop"></context:component-scan><!-- 开启自动生成代理--><aop:aspectj-autoproxy></aop:aspectj-autoproxy>
3.使用
首先还是需要调用IoC自动扫包
public class test {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("springaop.xml");Cal bean = context.getBean(Cal.class);System.out.println(bean.add(1,2));System.out.println(bean.sub(3,1));System.out.println(bean.mul(8,2));System.out.println(bean.div(4,2));}}
1
