


























<bean id="对象名" class="类的路径(org.springframework.abcdef)"></bean>
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class testIOC {
@Test
public void testMain() {
ApplicationContext context = new ClassPathXmlApplicationContext("a.xml");
Main m = (Main) context.getBean("m");
System.out.println(m);
m.add();
}
}
注:Maven自带的Junit版本为3.8.2,无法使用注解方式,故应手动修改为4.0以上版本才可使用注解。
测试结果如上图
Beans 实例化有三种方式:
创建对象的时候,向类里面的属性设置值
属性注入的三种方式:

<bean id="demo1" class="类">
<constructor-arg name="username" value="值"></constructor-arg>
</bean>
<bean id="demo2" class="类">
<property name="bookname" value="值"></property>
</bean>
<bean id=”userDao” class=”dao”></bean>
<bean id=”userService” class=”service”>
<!-- 不写value 写ref属性 对应ID -->
<property name=”userDao” ref=”userDao”></property>
</bean>
P名称空间注入:

数组与list集合
<bean id=”person” class=”类”>
<!-- 数组 list-->
<property name=”arrs”>
<list>
<value>aaa</value>
<value>ada</value>
<value>aba</value>
</list>
</property>
<!-- map -->
<property name=”map”>
<map>
<entry key=”aa” value=”lucy”></entry>
<entry key=”bb” value=”ldcy”></entry>
<entry key=”cc” value=”luacy”></entry>
</map>
</property>
<property name=”properties”>
<props>
<prop key=”driverclass”>com.mysql.jdbc.Driver</prop>
<prop key=”username”>user</prop>
……
</props>
</property>
</bean>

配置文件中,开启注解扫描
到包里面扫描类、方法、属性上面是否有注解
<context:component-scan base-package=”包名”></context:component-scan>
只扫描属性上面的注解
context:annotation-config
@Component(value=”user”) //<bean id=”user” class=”” />
Public class User{
……

创建service类,创建dao类
@Service(value=”userService”)
Public class UserService{
//得到dao对象
//定义dao类型属性
//在dao属性上面使用注解 完成对象注入
@Autowired
Private UserDao userDao;
//使用注解方式的时候不需要使用set方法
Public void add(){
……
userDao.add();
}
}
通过类名寻找对应的对象进来,自动注入/装配。 注入属性的两个对象
@Autowired 自动装配
@Resource(name) 注入指定对象,name为对象名
配置文件和注解混合使用
创建对象操作使用配置文件方式实现,然后注入属性的操作使用注解方式实现。
原理图
横向机制:
无接口情况:
使用动态代理实现。

来源:https://docs.spring.io/spring/docs/4.2.x/spring-framework-reference/html/xsd-configuration.html
<!--配置对象-->
<bean id=”book” class=”类”></bean>
<bean id=”myBook” class=”类”></bean>
<!--配置aop操作-->
<aop:config>
<!--配置切入点-->
<aop:pointcut expression=”execution(* cn.itcast.aop.Book.*(..))” id=”pointcut1” />
<!--配置切面-->
<aop:aspect ref=”myBook”>
<!--配置增强类型 method,增强类里面使用哪个方法作为前置-->
<!--前置-->
<aop:before method=”before1” pointcut-ref=”pointcut1”/>
<!--后置-->
<aop:after-returning method=”after1” pointcut-ref=”pointcut1” />
<!--环绕-->
<aop:around method=”around1” pointcut-ref=”pointcut1” />
</aop:aspect>
</aop:config>
环绕:
//MyBook类
Public void around1(ProceedingJoinPoint proceedingJoinPoint){
//方法之前
System.out.println(“方法之前”);
//执行被增强的方法
proceedingJoinPoint.proceed();
//方法之后
System.out.println(“方法之后”);
}
演示问题 每次访问action时,都会加载spring配置文件 action调用service,service调用dao
解决方案
在spring里面不需要我们自己写代码实现,帮忙封装
<!--Web.xml-->
<listener>
<listener-class>
Org.springframework.web.context.ContextLoaderListener
</listener-class>
</listenner>

此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。