您的位置:首页技术文章
文章详情页

Java基础之Spring5的核心之一IOC容器

浏览:3日期:2022-08-13 13:22:35
一、什么是IOC

1)控制反转,把创建对象和对象的调用过程交给Spring 管理。

2)使用IOC的目的,为了降低耦合度。

二、IOC的底层原理

XML解析、工厂模式、反射

Java基础之Spring5的核心之一IOC容器

三、IOC思想

基于IOC容器完成,IOC容器底层就是对象工厂。

四、Spring 提供IOC容器实现两种方式:(两个接口)

(1)BeanFactory:IOC容器基本实现,是Spring内部的使用接口,不提供开发人员使用

特点:加载配置文件的时候不会创建对象,在获取(使用)对象才去创建。

(2)ApplicationContext:BeanFactory接口的子接口。提供开发人员使用

特点:在加载配置文件的时候就创建对象

五、IOC操作之Bean管理

1、什么是Bean管理:

Bean管理指的是两个操作:Spring创建对象Spring注入值:手动注入、自动装配

<!--创建对象、自动装配 bean标签里面的autowire属性:属性值:byName:根据属性名字注入值,id的值必须与类里面的属性名称一样 byType:根据属性的类型注入值。--><bean autowire='byType'> <!--手动装配--> <!--<property name='dept' ref='dept'></property>--></bean>

2、IOC操作之Bean管理两种方式

1)基于XML配置文件方式:

首先创建一个User类:

public class User { private Integer id;private String name; public User() {} public User(Integer id, String name) { this.id = id; this.name = name;} public void setId(Integer id) { this.id = id;} public void setName(String name) { this.name = name;} public void setAddress(String address) { this.address = address;} }

在XML配置文件中配置User对象:

<!--1、配置User对象 id:为创建后的对象名,可自取 class:为类的全路径 --><bean class='tianfei.Spring5.User'> <!--2、通过set方法注入属性 通过property注入属性 name :类里面的属性 value :类里面属性对应的值 --> <property name='id' value='1'></property> <property name='name' value='田飞'></property> <!--通过有参构造注入属性--> <constructor-arg name='id' value='2'></constructor-arg> <constructor-arg name='name' value='张三'></constructor-arg> <!--通过p名称空间注入属性 使用前,需在xml标签属性中加入:xmlns:p='http://www.springframework.org/schema/p' --> <bean p: p:name='李四'></bean></bean>

测试类:

public class testDemo { @Test public void test() {//1、加载Spring5 的xml配置文件ApplicationContext context = new ClassPathXmlApplicationContext('bean1.xml');//2、获取配置创建的对象User user = context.getBean('usera', User.class);System.out.println(user); }}

补充:在XML配置文件中引入外部文件(以jdbc.properties)

<!--引入外部文件 jdbc.properties需在名称空间中加入:xmlns:context='http://www.springframework.org/schema/context' --> <context:property-placeholder location='classpath:jdbc.properties'></context:property-placeholder> <bean ><property name='driverClassName' value='${prop.driverClassName}'></property><property name='url' value='${prop.url}'></property><property name='username' value='${prop.username}'></property><property name='password' value='${prop.password}'></property> </bean>

jdbc.properties配置文件内容如下:

prop.driverClassName=com.mysql.jdbc.Driverprop.url=jdbc:mysql://localhost:3306/userdbprop.username=rootprop.password=tianfei

2)基于注解方式(以UserDao接口以及其实现类和UserService类为例)

public interface UserDao { public void add(); }@Controller(value = 'userDaoImpl') public class UserDaoImpl implements UserDao { @Overridepublic void add() { System.out.println('userdao add.......');} public UserDaoImpl() {} }

//value属性可以省略,如果省略则默认创建的对象名为 类名首字母小写 对象@Service(value = 'userService')public class UserService { //基于注解注入基本类型数据 @Value(value = 'abc') private String str; //基于注解注入对象类型值// @Autowired //根据类型注入// @Qualifier(value = 'userDaoImpl') //根据名称注入 需要和 Autowired 一起使用// private UserDao userDao;// @Resource //根据类型注入 @Resource(name = 'userDaoImpl') //根据名称注入 private UserDao userDao; public void add() {System.out.println('service add........');userDao.add(); } public void test() {System.out.println('userDao = ' + userDao);System.out.println(str); }}

测试类:

public class test { @Test public void test1(){//测试使用注解创建对象//需要在mxl配置文件中配置:<context:component-scan base-package='tianfei.Spring5'></context:component-scan> ,来开启注解扫描组件//不需要添加set方法ApplicationContext context = new ClassPathXmlApplicationContext('bean1.xml');UserService userService = context.getBean('userService', UserService.class);System.out.println(userService);userService.add();userService.test(); } @Test public void test2(){//测试使用注解创建对象//不需要添加set方法//使用完全注解ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);UserService userService = context.getBean('userService', UserService.class);System.out.println(userService);userService.add();userService.test(); }}

使用完全注解时需要创建一个类作为配置类:

@Configuration //作为配置类,代替xml配置文件,进行完全注解开发@ComponentScan(value = {'tianfei.Spring5'})public class SpringConfig {}

3、IOC操作Bean管理(工厂Bean)

Spring有两种Bean:

1)普通Bean:在配置文件中定义bean类型就是返回值类型

2)工厂Bean:在配置文件中定义bean类型和返回值类型可以不一样

第一步:创建一个类,作为工厂bean,实现接口FactoryBean

第二步:实现接口中得方法,在实现的方法中定义返回的bean类型

public class MyBean implements FactoryBean<User> { //用来改变返回对象(返回的对象可以和创建的对象不一样) @Override public User getObject() throws Exception {return new User(1,'张三'); } @Override public Class<?> getObjectType() {return null; } @Override public boolean isSingleton() {return false; }}

创建 MyBean.xml文件添入:

<!--创建类的对象--><bean class='tianfei.Spring5.factorybean.MyBean'></bean>

测试类:

public class test { @Test public void testMyBean() {//测试 FactoryBean 工厂ApplicationContext context = new ClassPathXmlApplicationContext('MyBean.xml');Book book = context.getBean('myBean',Book.class);System.out.println(book); }}

4、Bean的生命周期(加上后置处理器后共七步):

(1)通过无参构造器创建bean实例(无参构造)(2)为 bean的属性注入值或引用其他bean(set方法)(3)把 bean 实例传递给 bean 后置处理器方法 postProcessBeforeInitialization(4)调用 bean的初始化方法(需要自行配置初始化方法)(5)把 bean 实例传递给 bean 后置处理器方法 postProcessAfterInitialization(6)获取 bean实例对象(7)当容器关闭时,调用 bean的销毁方法(需要自行配置销毁方法)

到此这篇关于Java基础之Spring5的核心之一IOC容器的文章就介绍到这了,更多相关Java Spring5的核心IOC容器内容请搜索好吧啦网以前的文章或继续浏览下面的相关文章希望大家以后多多支持好吧啦网!

标签: Java
相关文章: