加粗样式# IOC控制反转
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">
</beans>
三种实例化对象的方法
构造器
<!--IOC inverse Of control-->
<!--UserService user=new UserService()-->
<!--
name=对象的名称
id:类对象的名称,推荐*****
class:类的全路径名称
scope:作用域,
singleton 单例的 默认值
prototype 原型的,非单例
request
session;
-->
<bean id="userService" name="us" class="com.yjxxt.service.UserService" scope="prototype"></bean>
<!--导入其他配置文件-->
<import resource="spring01.xml"></import>
静态工厂
在有一个静态工厂类情况下:
public class CarFactory {
/**
* 静态工厂
* @return
*/
public static Car getCar(String type){
return new Car(10001,type,2000000.01);
}
}
<!--静态工厂实例化-->
<bean id="car" class="com.yjxxt.factory.CarFactory" factory-method="getCar">
<constructor-arg value="BMW"></constructor-arg>
</bean>
实例化工厂
在有一个实例化工厂类的情况下:
public class CarFactory2 {
/**
* 实例化工厂
* @return
*/
public Car getCar(){
return new Car(10001,"BMW",2000000.01);
}
}
<!--实例化工厂对象-->
<bean id="factory" class="com.yjxxt.factory.CarFactory2"></bean>
<!--获取bean car-->
<bean id="c02" factory-bean="factory" factory-method="getCar"></bean>
依赖注入方式
set注入
<bean id="stu" class="com.yjxxt.bean.Student">
<property name="sid" value="20211001"></property>
<property name="name" value="冯杰"></property>
<property name="age" value="18"></property>
<property name="score" value="78.8"></property>
构造器注入
<bean id="user02" class="com.yjxxt.bean.User">
<!--name 代表属性名称,index 代表参数下标索引 -->
<!-- <constructor-arg name="uid" value="102"></constructor-arg>
<constructor-arg name="uname" value="lisi"></constructor-arg> -->
<constructor-arg index="0" value="102" ></constructor-arg>
<constructor-arg index="1" value="lisi006"></constructor-arg>
<!--参考 ioc容器的对象-->
<constructor-arg index="2" ref="date" ></constructor-arg>
</bean>
静态工厂注入
<bean id="car" class="com.yjxxt.factory.CarFactory" factory-method="getCar">
<constructor-arg value="BMW"></constructor-arg>
</bean>
实例化工厂注入
p标签注入
xmlns:p="http://www.springframework.org/schema/p"
<!--p标签注入-->
<bean id="stu002" class="com.yjxxt.bean.Student" p:myCar-ref="car">
</bean>
复杂属性的注入
有时创建对象的属性可能是个复杂的不能用一个value搞定,所以需要使用复杂的注入;
<!--日期对象-->
<bean id="date" class="java.util.Date">
<property name="year" value="2010"></property>
</bean>
<!--静态工厂实例化-->
<bean id="stu" class="com.yjxxt.bean.Student">
<property name="sid" value="20211001"></property>
<property name="name" value="冯杰"></property>
<property name="age" value="18"></property>
<property name="score" value="78.8"></property>
<!--参考-->
<property name="birth" ref="date"></property>
<!--注入set集合-->
<property name="aset">
<set>
<value>吸烟</value>
<value>喝酒</value>
<value>烫头</value>
</set>
</property>
<!--注入list集合-->
<property name="klist">
<list>
<value>AAAA</value>
<value>BBBB</value>
<value>CCCC</value>
</list>
</property>
<!--注入map-->
<property name="map">
<map>
<entry key="6222212345678">
<value>中国银行业</value>
</entry>
<entry key="6222212345678">
<value>工商银行业</value>
</entry>
<entry key="6222212345678">
<value>建设银行业</value>
</entry>
</map>
</property>
<!--注入properties-->
<property name="properties">
<props>
<prop key="driver">com.mysql.jdbc.Driver</prop>
<prop key="url">jdbc:mysql:///java50?characterEncoding=utf-8</prop>
<prop key="username">root</prop>
<prop key="password">root</prop>
</props>
</property>
<!--注入数组-->
<property name="hobby">
<array>
<value>语文</value>
<value>数学</value>
<value>外语</value>
</array>
</property>
<!--注入car-->
<property name="myCar" ref="car02"></property>
</bean>
自动装配
开启扫描
在spring.xml文件中写入:
<!--开启自动装配-->
<context:annotation-config/>
开启自动装配之后,在你要实例化的变量上面标上@Autowired
如果spring.xml中有设置好了的类型对应的bean,就自动拿过去与那个变量匹配;
public class UserController {
@Autowired
private UserServiceInf userService;
public void start(int id){
User user = userService.findById(id);
System.out.println(user);
}
}
如上代码就相当于已经实例化了一个UserService对象;
扫描开发
开启扫描,在spring.xml文件中写入:
<context:component-scan base-package="com.yjxxt"/>
在你所需要实例化的类前注解一个
@Controller//controller层注解
@Component//任意类注解
@Service//Service层
@Repository//dao层
注解之后就相当于这个类已经在xml文件中写入了一个bean,然后就可以配合自动装配引用;