(1)IOC 底层原理
(2)IOC 接口(BeanFactory)
(3)IOC 操作 Bean 管理(基于 xml)
(4)IOC 操作 Bean 管理(基于注解)
(1)控制反转,把对象的创建和对象之间的调用过程,交给 Spring 进行管理
(2)使用 IOC 的目的:为了耦合度降低
(3)做入门案例就是 IOC 的实现
(1)xml 解析、工厂模式、反射
<!--配置User对象创建-->
<bean id="user" class="com.gbx.spring5.User"></bean>
@SuppressWarnings({"all"})
public class Book {
//创建属性
private String b_name;
private String b_author;
//创建属性对应的set方法
public void setB_author(String b_author) {
this.b_author = b_author;
}
public void setB_name(String b_name) {
this.b_name = b_name;
}
}
<!--2 set方法注入属性 找的是无参-->
<bean id="book" class="com.gbx.spring5.Book">
<!--使用property完成属性注入
name:类里面属性名称
value:向属性注入的值-->
<property name="b_name" value="水浒"></property>
<property name="b_author" value="施耐庵"></property>
</bean>
@SuppressWarnings({"all"})
public class Orders {
private String oname;
private String address;
//创建有参构造
public Orders(String oname, String address) {
this.oname = oname;
this.address = address;
}
}
<bean id="orders" class="com.gbx.spring5.Orders">
<constructor-arg name="oname" value="abc"></constructor-arg>
<constructor-arg name="address" value="中国"></constructor-arg>
<!-- 通过索引找到属性 <constructor-arg index="0" value=""></constructor-arg>-->
</bean>
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"
<!-- 2 set方法注入属性-->
<bean id="book" class="com.gbx.spring5.Book" p:b_name="红楼" p:b_author="曹雪芹">
</bean>
<!--null值-->
<property name="address">
<null/>
</property>
<!--属性值包含特殊符号
1 把<>进行转义 用 < >
2 把带特殊符号内容写到CDATA
-->
<property name="address" >
<value><![CDATA[<<南京>>]]></value>
</property>
public class UserService {
//创建UserDao类型属性,生成set方法
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void add(){
System.out.println("service add");
//原始方式创建UserDao对象
//UserDao userDao = new UserDaoImpl();
userDao.update();
}
}
public interface UserDao {
public void update();
}
public class UserDaoImpl implements UserDao {
@Override
public void update() {
System.out.println("dao update");
}
}
<!--1 service和dao对象创建-->
<bean id="userService" class="com.gbx.spring5.service.UserService">
<!--注入userDao对象
name属性值:类里面属性名称
ref属性:创建userDao对象bean镖标签id值
-->
<property name="userDao" ref="userDaoImpl"></property>
</bean>
<bean id="userDaoImpl" class="com.gbx.spring5.dao.UserDaoImpl"></bean>
3.1.5 内部 bean
//部门类
public class Dept {
private String name;
public void setName(String name) {
this.name = name;
}
}
//员工类
public class Emp {
private String ename;
private String gender;
//员工属于某一个部门,使用对象形式表示
private Dept dept;
public void setEname(String ename) {
this.ename = ename;
}
public void setGender(String gender) {
this.gender = gender;
}
public void setDept(Dept dept) {
this.dept = dept;
}
}
<!--内部bean-->
<bean id="emp" class="com.gbx.spring5.bean.Emp">
<!--设置两个普通属性-->
<property name="ename" value="Lee"/>
<property name="gender" value="男"/>
<!--设置对象类型属性-->
<property name="dept">
<bean id="dept" class="com.gbx.spring5.bean.Dept">
<property name="name" value="开发部"/>
</bean>
</property>
</bean>
<!--级联赋值-->
<bean id="emp" class="com.gbx.spring5.bean.Emp">
<!--设置两个普通属性-->
<property name="ename" value="Lee"/>
<property name="gender" value="男"/>
<!--级联赋值-->
<property name="dept" ref="dept"/>
</bean>
<bean id="dept" class="com.gbx.spring5.bean.Dept">
<property name="name" value="技术部"/>
</bean>
<!--级联赋值-->
<bean id="emp" class="com.gbx.spring5.bean.Emp">
<!--设置两个普通属性-->
<property name="ename" value="Lee"/>
<property name="gender" value="男"/>
<!--级联赋值-->
<property name="dept" ref="dept"/>
<property name="dept.name" value="财务部"/>
</bean>
<bean id="dept" class="com.gbx.spring5.bean.Dept">
<property name="name" value="技术部"/>
</bean>
//员工类
public class Emp {
private String ename;
private String gender;
//员工属于某一个部门,使用对象形式表示
private Dept dept;
//生成dept的get方法
public void setEname(String ename) {
this.ename = ename;
}
public void setGender(String gender) {
this.gender = gender;
}
public void setDept(Dept dept) {
this.dept = dept;
}
public Dept getDept() {
return dept;
}
public void add(){
System.out.println(ename + "::" + gender + "::" + dept);
}
}
@SuppressWarnings({"all"})
public class Stu {
//1.数组类型属性
private String[] courses;
//2.list集合类型属性
private List<String> list;
//3.map集合类型属性
private Map<String,String> maps;
//4.set集合类型属性
private Set<String>sets;
public void setCourses(String[] courses) {
this.courses = courses;
}
public void setList(List<String> list) {
this.list = list;
}
public void setMaps(Map<String, String> maps) {
this.maps = maps;
}
public void setSets(Set<String> sets) {
this.sets = sets;
}
}
<!--1 集合类型属性注入-->
<bean id="stu" class="com.gbx.spring5.collectionType.Stu">
<!--数组类型属性注入-->
<property name="courses">
<array>
<value>java</value>
<value>python</value>
</array>
</property>
<!--lsit类型属性注入-->
<property name="list">
<list>
<value>Lee</value>
<value>Lee浅辄</value>
</list>
</property>
<!--map属性注入-->
<property name="maps">
<map>
<entry key="Java" value="java"/>
<entry key="PHP" value="php"/>
</map>
</property>
<!--set属性注入-->
<property name="sets">
<set>
<value>Mysql</value>
<value>MongoDB</value>
</set>
</property>
</bean>
<!--创建多个course对象-->
<bean id="course1" class="com.gbx.spring5.collectionType.Course">
<property name="cname" value="Spring 5框架"/>
</bean>
<bean id="course2" class="com.gbx.spring5.collectionType.Course">
<property name="cname" value="MyBatis框架"/>
</bean>
<!--注入list集合类型,值是对象-->
<property name="courseList">
<list>
<ref bean="course1"/>
<ref bean="course2"/>
</list>
</property>
<?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:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<!--这里引入的是util
然后复制一下http那行,并把里面的beans都改为util
-->
<!--1 提取1ist集合类型属性注入-->
<util:list id="bookList">
<!-- 写入对象 <ref bean=""/>-->
<value>西游</value>
<value>红楼</value>
<value>三国</value>
<value>水浒</value>
</util:list>
<!--这里只是以list集合为例,想注其他类型 util:(类型)-->
<!--2 提取1ist集合类型属性注入使用-->
<bean id="book" class="com.gbx.spring5.collectionType.Book">
<property name="list" ref="bookList"/>
</bean>
public class MyBean implements FactoryBean<Course> {
//定义返回bean
@Override
public Course getObject() throws Exception {
Course course = new Course();
course.setCname("abc");
return course;
}
@Override
public Class<?> getObjectType() {
return null;
}
@Override
public boolean isSingleton() {
return FactoryBean.super.isSingleton();
}
}
<bean id="myBean" class="com.gbx.spring5.factorybean.MyBean">
</bean>
@Test
public void test3(){
ApplicationContext context =
new ClassPathXmlApplicationContext("bean3.xml");
Course course = context.getBean("myBean", Course.class);
System.out.println(course);
}
@SuppressWarnings({"all"})
public class Orders {
//无参数构造
public Orders(){
System.out.println("第一步 创建无参数构造创建bean实例");
}
private String oname;
public void setOname(String oname) {
this.oname = oname;
System.out.println("第二步 调用set方法设置属性值");
}
//创建执行初始化的方法
public void initMethod(){
System.out.println("第三步 执行初始化方法");
}
//创建执行销毁的方法
public void destoryMethod(){
System.out.println("第五步 执行销毁方法");
}
}
<bean id="orders" class="com.gbx.spring5.bean.Orders" init-method="initMethod" destroy-method="destoryMethod">
<property name="oname" value="手机"/>
</bean>
@Test
public void testBean3(){
// ApplicationContext context =
// new ClassPathXmlApplicationContext("bean4.xml");
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext("bean4.xml");
Orders orders = context.getBean("orders", Orders.class);
System.out.println("第四步 获取创建bean实例对象");
System.out.println(orders);
//手动让bean实例销毁
context.close();
}
public class MyBeanPost implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("在初始化之前执行的方法");
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("在初始化之后执行的方法");
return bean;
}
}
<!--配置后置处理器-->
<bean id="myBeanPost" class="com.gbx.spring5.bean.MyBeanPost"></bean>
<!--实现自动装配
bean标签属性autowire,配置自动装配
autowire属性常用两个值:
byName根据属性名称注入,注入值bean的id值和类属性名称一样
byType根据属性类型注入
-->
<bean id="emp" class="com.gbx.spring5.autowire.Emp" autowire="byName">
<!--<property name="dept" ref="dept"/>-->
</bean>
<bean id="dept" class="com.gbx.spring5.autowire.Dept">
</bean>
<!--实现自动装配
bean标签属性autowire,配置自动装配
autowire属性常用两个值:
byName根据属性名称注入,注入值bean的id值和类属性名称一样
byType根据属性类型注入
-->
<bean id="emp" class="com.gbx.spring5.autowire.Emp" autowire="byType">
<!--<property name="dept" ref="dept"/>-->
</bean>
<bean id="dept" class="com.gbx.spring5.autowire.Dept">
<!--直接配置连接池-->
<bean id="dateSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/db01"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
</bean>
<?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:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
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/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--引入外部属性文件-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--直接配置连接池-->
<bean id="dateSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${prop.driverClass}"></property>
<property name="url" value="${prop.url}"></property>
<property name="username" value="${prop.username}"></property>
<property name="password" value="${prop.password}"></property>
</bean>
<!--开启组件扫描
1 如果扫描多个包,多个包使用逗号隔开
2 扫描包上层目录
-->
<context:component-scan base-package="com.gbx.spring5"></context:component-scan>
/在注解里面value属性值可以省略不写
//默认值是类名称,首字母小写
//UserService --userService
@Component(value = "userService") //<bean id='userService"class="..">
public class UserService {
public void add(){
System.out.println("service add");
}
}
<!--开启组件扫描
1 如果扫描多个包,多个包使用逗号隔开
2 扫描包上层目录
-->
<context:component-scan base-package="com.gbx.spring5"></context:component-scan>
<!--示例1
use-default-filters=“"false"表示现在不使用默认filter,自己配置filter
context:include-filter:设置扫描哪些内容
-->
<context:component-scan base-package="com.gbx" use-default-filters="false">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--示例2
下面配置扫描包所有内容
context:exclude-filter:设置哪些内容不进行扫描
-->
<context:component-scan base-package="com.gbx">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
@Repository
public interface UserMapper {
public void add();
}
public class UserMapperImpl implements UserMapper {
@Override
public void add() {
System.out.println("mapper add..");
}
}
@Service
public class UserService {
//定义mapper类型属性
//不需要添加set方法
//添加注入属性注解
@Autowired
private UserMapper userMapper;
public void add(){
System.out.println("service add");
userMapper.add();
}
}
@Autowired
@Qualifier(value = "userMapperImpl1")//根据名称进行注入
private UserMapper userMapper;
//@Resource //根据类型进行注入
@Resource(name = "userMapperImpl1")
private UserMapper userMapper;
@Value(value = "abc")
private String name;
@Configuration//作为配置类,替代xml配置文件
@ComponentScan(basePackages = {"com.gbx.spring5"})
public class SpringConfig {
}
@Test
public void testService2(){
//加载配置类
ApplicationContext context =
new AnnotationConfigApplicationContext(SpringConfig.class);
UserService userService = context.getBean("userService", UserService.class);
System.out.println(userService);
userService.add();
}