spring控制反转,对象的控制权的反转,本来在开发者手中,现在在容器那里。
spring在加载配置文件的时候,已经将对象初始化,然后放在Spring容器中,用的时候给Spring容器要就好了。有多种的方式
spring的配置文件有两种不同的加载方式
User user1 = (User)context.getBean("user"); //按照名字
User user2 = context.getBean("user", User.class);//按照名字
User user3 = context.getBean(User.class); //按照类型
//从classpath下加载,在maven中就是从resources中加载
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//从绝对路径下加载
FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext("D:\\code\\tqq-study\\tqq-ssm\\spring-ioc01\\src\\main\\resources\\applicationContext.xml");
2、Spring 的属性注入
2.1、构造方法注入
<bean class="com.tqq.ioc.model.User" id="user">
<constructor-arg name="name" value="tqq"></constructor-arg>
<constructor-arg name="address" value="上海"></constructor-arg>
<constructor-arg name="id" value="9"></constructor-arg>
</bean>
2.2、set 方法注入 (用的最多的一种方式)
<bean class="com.tqq.ioc.model.User" id="user2">
<property name="address" value="江苏"></property>
<property name="id" value="3"></property>
<property name="name" value="tqq2"></property>
</bean>
2.3、p命名空间注入
本质上也是调用了set方法
<bean class="com.tqq.ioc.model.User" p:name="tqq3" p:address="浙江" p:id="5" id="user3"></bean>
2.4、外部bean 注入
复杂属性的注入
对象注入
<bean class="com.tqq.ioc.model.Cat" id="cat">
<property name="name" value="Tom"></property>
<property name="age" value="5"></property>
</bean>
数组注入
<bean class="com.tqq.ioc.model.People" id="people2">
<property name="name" value="壮壮"></property>
<property name="cat" ref="cat"></property>
<property name="cats">
<array>
<ref bean="cat"></ref>
<bean class="com.tqq.ioc.model.Cat" id="cat2">
<property name="name" value="tom2"></property>
<property name="age" value="3"></property>
</bean>
</array>
</property>
<property name="favorites">
<list>
<value>足球</value>
<value>拳击</value>
</list>
</property>
</bean>
Map注入
<property name="details">
<map>
<entry key="gender" value="男"></entry>
<entry key="age" value="10"></entry>
</map>
</property>
Properties注入
<property name="info">
<props>
<prop key="phone">1234</prop>
</props>
</property>
三、Java 配置
在spring中将bean注入到Spring容器中有三种方式
- XML
- Java配置
- 自动化扫描
@Configuration //注解表示这是一个Java配置类,配置类的作用类似于applicationContext.xml
public class JavaConfig {
@Bean
SayHello sayHello(){
return new SayHello();
}
}
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(JavaConfig.class);
SayHello sayHello = context.getBean("sayHello", SayHello.class);
System.out.println(sayHello.sayHello("tqq"));
自动化配置
可以通过Java配置或者XML配置实现
Java代码实现
给类添加注解
@Service
@Component
@Repository
@Controller
1、加注解
@Service
public class UserService {
public List<String> getAllUsers(){
ArrayList<String> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
list.add("tqq:"+i);
}
return list;
}
}
2、配置包扫描
@Configuration //注解表示这是一个Java配置类,配置类的作用类似于applicationContext.xml
@ComponentScan("com.tqq.ioc.service")
public class JavaConfig {
@Bean
SayHello sayHello(){
return new SayHello();
}
}
3、测试
public class JavaConfigTest {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(JavaConfig.class);
UserService userService = context.getBean(UserService.class);
List<String> users = userService.getAllUsers();
System.out.println(users);
}
}
xml实现
<context:component-scan base-package="com.tqq.ioc.service">
</context:component-scan>
在启动的时候要加载配置文件
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
对象注入
自动扫描时的对象注入有三种方式
@Autowired 根据类型查找 可以配合@Qualifier使用
@Resources 根据名称查找
@Injected