构造器注入
给构造方法的形参赋值
index索引注入
<constructor-arg index="0" value="123"></constructor-arg>
name 参数名字注入
<constructor-arg name="id" value="456"></constructor-arg>
type类型注入
<constructor-arg type="java.lang.Long" value="111"></constructor-arg>
值的注入
通过name注入
<property name="id" value="111"></property>
<property name="name" value="康康"></property>
给数组注入值
<property name="arrays">
<array>
<value>A1</value>
<value>A2</value>
<value>A3</value>
</array>
</property>
给List/Set注入值
<property name="list/set">
<list/set>
<value>mm</value>
<value>hh</value>
<value>mm</value>
</list/set>
</property>
给properties注入值
<property name="prop2">
<props>
<prop key="key值">值</prop>
<prop key="username">qiang强</prop>
</props>
</property>
组件扫描器
<context:component-scan base-package="需要被扫描的包"></context:component-scan>
如果一个接口有两个实现情况下
方案一:一个接口有多个实现的情况下面 通过名字去区分
方案二:通过Resource这个注解
区别:@Autowired和@Qualifier 都是属于spring的注解 ,
Resource使用jdk的注解 推荐使用Autowired和Qualifier,可以和spring进行无缝衔接
autowired默认是根据类型匹配,如果类型匹配不上在根据名字匹配
而Resource默认根据名字匹配,名字匹配不上就匹配类型
SpringAop配置方式
xml版本的配置
<context:component-scan base-package="_04_aopxml"></context:component-scan>
<aop:config>
<aop:pointcut id="pointcut" expression="execution(* _04_aopxml.I*Service.*(..))"></aop:pointcut>
<!--方式一-->
<!--配置通知-->
<aop:aspect ref="txManager">
<!-- 前置通知-->
<aop:before method="begin" pointcut-ref="pointcut"></aop:before>
<!– 后置通知–>
<aop:after-returning method="commit" pointcut-ref="pointcut"></aop:after-returning>
<!– 最终通知–>
<aop:after method="close" pointcut-ref="pointcut"></aop:after>
<!– 异常通知–>
<aop:after-throwing method="rollback" throwing="e" pointcut-ref="pointcut"></aop:after-throwing>
<!--方式二(推荐)-->
<!-- 环绕通知-->
<aop:around method="around" pointcut-ref="pointcut"></aop:around>
</aop:aspect>
</aop:config>
<bean id="txManager" class="_04_aopxml.TxManager"></bean>
注解版本配置
@Component
@Aspect
public class TxManager {
@Pointcut("execution(* _05_aopanno.I*Service.*(..))")
public void pointcut(){}
//<aop:before method="begin" pointcut-ref="pointcut"></aop:before>
@Before("pointcut()")
public void begin(){
System.out.println("开启事务");
}
@After("pointcut()")
public void commit(){
System.out.println("提交事务");
}
@AfterReturning("pointcut()")
public void close(){
System.out.println("关闭");
}
//所有的异常的父类
@AfterThrowing(value = "pointcut()",throwing = "e")
public void rollback(Throwable e){
System.out.println("回滚事务"+e.getMessage());
}
//环绕通知
@Around("pointcut()")
public void around(ProceedingJoinPoint joinPoint){
try {
begin();
//调用方法
joinPoint.proceed();
commit();
} catch (Throwable throwable) {
throwable.printStackTrace();
}finally{
close();
}
}
}
Bean的创建方式
以前模式:通过new 对象方式创建
spring方式 :spring帮我们创建bean 。–创建bean的方式
创建方式一
通过无参数的构造方法来创建bean --最常用方式
配置
<bean id="mybean" class="_06_bean.Mybean"></bean>
创建方式二
FactoryBean
public class MybeanFactoryBean implements FactoryBean<Mybean> {
//返回对象
public Mybean getObject() throws Exception {
return new Mybean("xxx", "18");
}
public Class<?> getObjectType() {
return Mybean.class;
}
//是否单例
public boolean isSingleton() {
return true;
}
}
创建方式三
通过类里面定义静态方法 来获取bean
public class MybeanFactory {
public static Mybean getBean(){
return new Mybean();
}
}
<bean class="_06_bean.MybeanFactory" factory-method="getBean"></bean>
创建方式四
public class MybeanFactory1 {
//普通方式
public Mybean getBean1(){
return new Mybean();
}
}
<bean class="_06_bean.MybeanFactory1" id="mybeanFactory1"></bean>
<bean factory-bean="mybeanFactory1" factory-method="getBean1"></bean>