Spring个人理解
Spring解决的主要问题
不使用Spring的情况下,类中的各种声明引用需要在使用时通过手动添加set或者用constructor进行注入,每个类之间的关系紧密,假如引用是一个接口,那么注入的时候采用了一个该接口的实现类,后期想要更换注入另一个实现类那么代码的修改将会变得非常繁琐,牵一发而动全身,而采用了Spring以后该注入将会由Spring帮助实现,只需要修改配置文件将引用所需要的依赖放入Spring容器中即可,Spring会在容器中寻找实现了该引用的bean将它注入到需要的bean中,这时类与类之间的控制关系将变成类与Spring的控制关系.,解耦会让使用者变得非常happy.
IOC
IOC的概念 IOC是一个容器,里面用来装Spring的各种bean,通俗的说就是各个类的实例, 以及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 http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="a" class="xxx.xxx.A"></bean>
</beans>
配置方式 java式
@Configuration
public class A{
@Bean
public B
getB(){
return new B();
}
}
实例化的几种方式
默认Bean采用无参构造创造实例
XML式
<bean id="a" class="xxxx.xxx.A"></bean>
静态Bean 使用某一个类中的某个静态方法得到的是另一个类中的实例
<bean id="b" class="xxx.xxx.A" factory-method="getB"></bean>
工厂Bean 指定一个工厂Bean的某一个方法得到另外的Bean
<bean id="a" class="xxx.xxx.A"></bean>
<bean id="b" factory-bean="a" factory-method="getB"></bean>
Spring Bean 通过将某一个java类实现FactoryBean接口,重写getObject方法获得的对象作为Bean,然后在XML配置文件中设置
public class A implements FactoryBean<B> {
@Override
public B
getObject() throws Exception
{
B b
= new B();
return b
;
}
<bean id="a" class="xxx.xxx.A"></bean>
通过包扫描+注解的方式得到Bean
@Cotroller注解Controller层想要加入IOC容器的类,同理@Service,@Repository(Dao层),@Component(自定义)
XML+注解
@Controller
public class A{}
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="xxx.xxx.xxx />
</beans>
java+注解
@Configuration
@ComponentScan("xxx.xxx.xxx")
public class Config{}
依赖注入的几种方式
在XML中指定
通过在类中加入set方法注入
<bean id="test" class="xxx.xxx.Test.class"></bean>
<bean id="a" class="xxx.xxx.A">
<property name="t" ref="test"></property>
</bean>
通过构造器注入
<bean id="test" class="xxx.xxx.Test"></bean>
<bean id="a" class="xxx.xxx.A">
<constructor-arg name="t" ref="test"></constructor-arg>
</bean>
在属性上加入@Autowired
@Controller
public class A{
@Autowired
private B b
;
}
Bean的生命周期和作用域
Bean的作用域指定的是一个Bean只有一个对象或者多个对象
单例模式
<bean id="a" class="xxx.xxx.A" scope="singleton"></bean>
@Bean
@Scope("singleton")
public A
a(){
return new A();
}
多例模式(原型模式)
<bean id="a" class="xxx.xxx.A" scope="prototype"></bean>
@Bean
@Scope("prototype")
public A
a(){
return new A();
}
声明周期指的是IOC容器初始化和销毁时执行的方法
<bean id="a" class="xxx.xxx.A" init-method="xxx" destroy-method="xxx"></bean>
@Bean(initMethod
= "innit",destroyMethod
= "close")
public A
a(){
return new A();
}
IOC的优势
spring IOC的好处是,对象的构建如果依赖非常多的对象,且层次很深,外层在构造对象时很麻烦且不一定知道如何构建这么多层次的对象。 IOC 帮我们管理对象的创建,只需要在配置文件里指定如何构建,每一个对象的配置文件都在类编写的时候指定了,所以最外层对象不需要关心深层次对象如何创建的,前人都写好了。
AOP的概念,应用场景和配置方式
概念
aop指的是面向切面编程,将指定的类需要的一些系统功能抽取出来作为代理类,在不破坏类的原有结构情况下,执行指定方法时加入到方法执行中作为额外的功能.
应用场景
比如日志记录、性能统计、事务管理、安全检查等等。
配置
XML式
<aop:config>
<aop:aspect ref="txAdvice">
<aop:pointcut id="servicePointcut" expression="execution(* com.woniuxy.spring.aop.service.impl.*.*(..))"/>
<aop:before method="begin" pointcut-ref="servicePointcut"/>
<aop:after-returning method="commit" pointcut-ref="servicePointcut"/>
<aop:after-throwing method="rollback" pointcut-ref="servicePointcut"/>
</aop:aspect>
</aop:config>
java式
@Configuration
@ComponentScan("xxx.xxx.xxx")
@EnableAspectJAutoProxy
public class TransactionConfig {}
@Service
public class A(){
public void test(){
}
}
@Component
@Aspect
public class TransactionAdvice {
@Pointcut("execution(* xxx.xxx.xxx.A.*(..))")
public void service(){}
@Around("service()")
public Object
invoke(ProceedingJoinPoint proceedingJoinPoint
){
Object
[] args
= proceedingJoinPoint
.getArgs();
Object obj
= null
;
try {
System
.out
.println("开启事务");
obj
= proceedingJoinPoint
.proceed(args
);
System
.out
.println("关闭事务");
} catch (Throwable throwable
) {
throwable
.printStackTrace();
System
.out
.println("事务回滚");
}
return obj
;
}
}