一、IOC的XML方式
1.工厂方法注入参数 XML方式
<bean id="CarFactory" class="com.testSpring.ioc.CarFactory" factory-method="CreateCar" >
<constructor-arg value="50"></constructor-arg>
</bean>
2.通过P名称空间注入属性(xmlns:p="http://www.springframework.org/schema/p" 引入P名称空间)
<bean id="People" class="com.testSpring.ioc.People" p:name="zhz" p:age="22"></bean>
</beans>
3.注入集合属性
<bean id="NameList" class="com.testSpring.ioc.NameList">
<property name="list">
<list>
<value>zxc</value>
<value>zxc</value>
<value>zxc</value>
</list>
</property>
<property name="arr">
<list>
<value>str</value>
<value>str</value>
<value>str</value>
</list>
</property>
<property name="map">
<map>
<entry key="1" value="1v"></entry>
</map>
</property>
</bean>
一、IOC的注解方式
0.准备工作
在配置文件中声明: 在base-package中,设置扫描的包
<?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 http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.testSpring.*"/>
</beans>
1.交给spring进行管理的类的声明()
由@Component(“id”)//对应于XML方式中<bean id="id" class=“”>,这个注解在类的声明上一行。
衍生的三种
@Repository("命名名称"):对应于DAO层
@Service("命名名称"):对应于service层
@Controller("命名名称"):对应于web层
2.注入属性
2.1普通的值属性
@Value("值")
2.2对象类型
方法1:@Autoware和@Qualifier(value="声明过的ID名称")。在对应属性上一行,源于spring框架
方法2:@Resource(name="ID名称"),源于spring接口的实现
三、注解和XML的联合使用
xml中配置<context:annoation-config/>,并且取出context:component-scanf的配置