1. set 注入
-
IOC容器,里面保存并注册了我们所有的Spring的对象 bean
-
在加载容器的时候,对象就被创建了
- 导入依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
- 编写实体类,需要set方法
public class User{
private String name;
public void setName(String name){
this.name = name;
}
public void show(){
System.out.println("name="+name);
}
}
- 注册bean
<bean id="user" class="com.yang.pojo.User">
<property name="name" value="yangkaibo"/>
</bean>
- 测试
@Test
public void test3(){
//IOC容器,里面保存并注册了我们所有的Spring的对象 bean
//在加载容器的时候,对象就被创建了
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
System.out.println("------------------------");
User user = (User)context.getBean("user");
user.show();
}
2. 构造器注入
- 导入依赖
- 编写实体类,不需要set方法
public class User{
private String name;
public User(String name) {
this.name = name;
}
public void show(){
System.out.println("name="+name);
}
}
- 注册到bean,三种模式
<constructor-arg name="name" value="moji"/>
<!--<constructor-arg index="0" value="moji2"/>-->
<!--<constructor-arg type="java.lang.String" value="moji3"/>-->
- 测试
@Test
public void test3(){
//IOC容器,里面保存并注册了我们所有的Spring的对象 bean
//在加载容器的时候,对象就被创建了
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
System.out.println("------------------------");
User user = (User)context.getBean("user");
user.show();
}
<bean id="user" class="com.yang.pojo.User">
<!--<property name="name" value="yangkaibo"/>-->
<constructor-arg name="name" value="moji"/>
<constructor-arg name="user" ref="moji"/>
</bean>
<!--如果是对象的话,用ref引用,其他的用value-->
<bean id="moji" class="com.yang.pojo.User">
</bean>
起别名的操作,凭自愿
<alias name="user" alias="yonghu"/>
import在多人协作中,通过import进行导入bean
<!--在多人协作中,通过import进行导入bean-->
<import resource="student.xml"/>
3.完善注入信息,bean就是在创建对象
- 编写地址类,使用get set方法
package com.yang.pojo;
public class Address {
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
- 编写学生类,提供要写入的参数类型
package com.yang.pojo;
import java.util.*;
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> list;
private Map<String,String> map;
private Set<String> set;
private String wife;
private Properties info;//配置类
public void setName(String name) {
this.name = name;
}
public void setAddress(Address address) {
this.address = address;
}
public void setBooks(String[] books) {
this.books = books;
}
public void setList(List<String> list) {
this.list = list;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
public void setSet(Set<String> set) {
this.set = set;
}
public void setWife(String wife) {
this.wife = wife;
}
public void setInfo(Properties info) {
this.info = info;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", address=" + address.getAddress() +
", books=" + Arrays.toString(books) +
", list=" + list +
", map=" + map +
", set=" + set +
", wife='" + wife + '\'' +
", info=" + info +
'}';
}
}
- 编写完整的注入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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="addr" class="com.yang.pojo.Address">
<property name="address" value="西安"/>
</bean>
<!--<bean id="user" class="com.yang.pojo.User">-->
<!--<property name="name" value="yao"/>-->
<!--</bean>-->
<!--scope 属性
singleton :在容器中只有一个对象(默认的,单例)
prototype :每次创建新对象
request/session 只在web中使用
-->
<bean id="student" class="com.yang.pojo.Student" scope="singleton">
<!--常量-->
<property name="name" value="杨凯波"/>
<!--引用类型-->
<property name="address" ref="addr"/>
<!--数组类型-->
<property name="books">
<array>
<value>西游记</value>
<value>水浒传</value>
<value>红楼梦</value>
</array>
</property>
<property name="list">
<list>
<value>list1</value>
<value>list2</value>
<value>list3</value>
</list>
</property>
<property name="map">
<map>
<entry key="key1" value="v1"/>
<entry key="key2" value="v2"/>
</map>
</property>
<property name="set">
<set>
<value>set1</value>
<value>set2</value>
<value>set3</value>
</set>
</property>
<property name="wife">
<null/>
</property>
<property name="info">
<props>
<prop key="id">100</prop>
<prop key="name">yaoyao</prop>
<prop key="sex">mybaby</prop>
</props>
</property>
</bean>
</beans>
- 测试
4.自动装配
- 新建entity包,创建三个实体类
public class Cat {
public void shot(){
System.out.println("我的");
}
}
public class Dog {
public void shut(){
System.out.println("对啊,你的");
}
}
private String name;
private Cat cat;
private Dog dog;
public void setName(String name) {
this.name = name;
}
public void setCat(Cat cat) {
this.cat = cat;
}
public void setDog(Dog dog) {
this.dog = dog;
}
public String getName() {
return name;
}
public Cat getCat() {
return cat;
}
public Dog getDog() {
return dog;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", cat=" + cat +
", dog=" + dog +
'}';
}
- 编写user.xml文件,注册bean
<bean id="dog" class="com.yang.entity.Dog"/>
<bean id="cat" class="com.yang.entity.Cat"/>
<!--<!–<!–<bean id="cat2" class="com.yang.entity.Cat"/>–>–>-->
<!--autowire="byName":自动配置 会自动寻找和属性相关的bean,本质是set方法-->
<!--autowire="byType":自动配置 会自动寻找和属性相关的bean,本质是set方法
如果这个类型有多个bean,就会报NoUniqueBeanDefinitionException错误,好理解,就是同一个类创建了多个对象,按照对象划分肯定是不行的-->
<bean id="user" class="com.yang.entity.User" autowire="byName">
<!--<property name="cat" ref="cat"/>-->
<!--<property name="dog" ref="dog"/>-->
<property name="name" value="yangkaibo"/>
</bean>
- 测试
@Test
public void test5(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
User user = (User) context.getBean("user");
System.out.println(user);
user.getCat().shot();
user.getDog().shut();
}
5.自动装配
为了支持注解,在applicationContext.xml文件中引入新的约束文件
1.增加 xmlns:context=“http://www.springframework.org/schema/context”
2.增加 http://www.springframework.org/schema/context
3开启注解支持 context:annotation-config/
- 编写User类增加注解
public class User {
private String name;
@Autowired
// @Qualifier("cat2")//组合注解,指定注入
private Cat cat;
//自动装配,默认按照byName来查找,如果byName不对,也会按照类型来查找
//required = false 表示允许对象为null,默认为true;
//spring的注解
@Resource//java的注解
private Dog dog;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public Cat getCat() {
return cat;
}
public Dog getDog() {
return dog;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", cat=" + cat +
", dog=" + dog +
'}';
}
}
- 注册bean
<bean id="dog" class="com.yang.entity.Dog"/>
<bean id="cat" class="com.yang.entity.Cat"/>
<!--<!–<!–<bean id="cat2" class="com.yang.entity.Cat"/>–>–>-->
<!--autowire="byName":自动配置 会自动寻找和属性相关的bean,本质是set方法-->
<!--autowire="byType":自动配置 会自动寻找和属性相关的bean,本质是set方法
如果这个类型有多个bean,就会报NoUniqueBeanDefinitionException错误,好理解,就是同一个类创建了多个对象,按照对象划分肯定是不行的-->
<bean id="user" class="com.yang.entity.User">
<!--<property name="cat" ref="cat"/>-->
<!--<property name="dog" ref="dog"/>-->
<property name="name" value="yangkaibo"/>
</bean>
- 测试
6 .使用注解开发
案例1
- 导入 Aop 的包,在导Spring 的包时,AOP的包已经导进去了
- 配置xml配置文件,开启注解支持
<context:annotation-config/>
<!--@Component相当于bean-->
<context:component-scan base-package="com.yang.pojo"/>
- 在实体类上增加注释
//相当于bean
@Component("user")
@Scope("prototype")
/*
* @Component("user") <bean id=user/>
* dao 等价 @Repository
* service 等价 @Service
* contrller 等价 @Controller
* */
public class User {
@Value("yang")
public String name;
}
- 测试
案例2
-
新建一个maven空项目
-
建立实体pojo包 和 配置文件config包
-
利用标签创建实体类
package com.yang.pojo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
//配置类
@Configuration//等价于Dog.xml
public class Dog {
@Value("乐乐")
public String name;
}
package com.yang.config;
import com.yang.pojo.Dog;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Configuration//标注是配置文件
@Import(MyConfig.class)//等价于import标签
public class ApplicationContext2 {
//返回一个bean id=方法名, class = 具体返回的对象
@Bean
public Dog dog(){
return new Dog();
}
}
- 测试
public class Testnew {
@Test
public void test1(){
ApplicationContext context = new AnnotationConfigApplicationContext(ApplicationContext2.class);
Dog dog = (Dog) context.getBean("dog");
System.out.println(dog.name);
}
}