Java 中的控制反转(IOC)详解
更新时间:2021年10月12日 16:00 点击:2045
IOC理论推导
Dao层
1.UserDao 接口
2.UserDaoImpl 实现类
Service层
3.UserService 业务接口
4.UserServiceImpl 业务实现类
用户实际调用service层 不会动dao层!
Dao层接口创建
package com.ckm.dao; public interface UserDao { void getUser(); }
Dao层实现类1创建
package com.ckm.dao; public class UserDaoImpl implements UserDao{ @Override public void getUser() { System.out.println("获取了User数据"); } }
Dao层实现类2创建
package com.ckm.dao; public class UserDaoMysqlImpl implements UserDao{ @Override public void getUser() { System.out.println("mysql使用了数据"); } }
Service层接口创建
package com.ckm.service; public interface UserService { void getUser(); }
Service层接口实现类创建
package com.ckm.service; import com.ckm.dao.UserDao; import com.ckm.dao.UserDaoImpl; public class UserServiceImpl implements UserService{ // private UserDao userDao = new UserDaoImpl(); //利用set进行动态实现值的注入 private UserDao userDao; public void setUserDao(UserDao userDao){ this.userDao = userDao; } @Override public void getUser() { userDao.getUser(); } }
单元测试
import com.ckm.dao.UserDaoImpl; import com.ckm.dao.UserDaoMysqlImpl; import com.ckm.service.UserServiceImpl; public class TestIOC { public static void main(String[] args) { //用户实际调用service层 不会动dao层! UserServiceImpl userService = new UserServiceImpl(); userService.setUserDao(new UserDaoMysqlImpl()); userService.getUser(); } }
Spring管理对象
resources中新建beans.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"> <!--使用Spring来创建对象,在Spring这些都称为Bean--> <!--bean = 对象--> <!--id = 变量名--> <!--class = new的对象--> <!--property 相当于给对象中的属性设值--> <bean id="DaoImpl" class="com.ckm.dao.UserDaoImpl" /> <bean id="MysqlImpl" class="com.ckm.dao.UserDaoMysqlImpl" /> <bean id="UserServiceImpl" class="com.ckm.service.UserServiceImpl"> <!-- value具体的值,基本数据类型 ref是引用Spring中创建好的对象 用户需要什么 就直接修改ref就ok --> <property name="userDao" ref="MysqlImpl" /> </bean> </beans>
dao层、Service层代码不变
测试类
import com.ckm.service.UserServiceImpl; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestIOC { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); UserServiceImpl userServiceImpl = (UserServiceImpl) context.getBean("UserServiceImpl"); userServiceImpl.getUser(); } }
Spring管理对象的简单例子
User类
package com.ckm.pojo; public class Hello { private String str; public String getStr() { return str; } public void setStr(String str) { this.str = str; } @Override public String toString() { return "Hello{" + "str='" + str + '\'' + '}'; } }
beans.xml
package com.ckm.pojo; public class Hello { private String str; public String getStr() { return str; } public void setStr(String str) { this.str = str; } @Override public String toString() { return "Hello{" + "str='" + str + '\'' + '}'; } }
测试类
import com.ckm.pojo.Hello; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MyTest { public static void main(String[] args) { //获取spring的上下文对象 //xml加载必须new ClassPathXmlApplicationContext() ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); //我们的对象现在都在spring中管理,我们要使用,直接去里面取出来 Hello hello = (Hello) context.getBean("hello"); System.out.println(hello.getStr()); } }
Bean无参构造类创建和有参构造类创建
当getBean的时候,这个类就已经被实例化了,就会执行无参构造方法
有参构造的User类
package com.ckm.pojo; public class User { private String name; // public User() { // System.out.println("无参构造"); // } public User(String name){ this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } public void show(){ System.out.println("name="+name); } }
编写Beans.xml
<!--第一种 下标赋值--> <bean id="user" class="com.ckm.pojo.User"> <constructor-arg index="0" value="这个是第一种有参设置的name"/> </bean>
<!--第二种 直接通过参数名--> <bean id="user" class="com.ckm.pojo.User"> <constructor-arg name="name" value="这个是第二种方法是有参设置的name"/> </bean>
Spring的一些配置
别名
<!--alias是别名,我们可以使用这个别名来获取这个对象--> <alias name="user" alias="userNew"/>
Bean的配置
<!--在bean中name也是别名,可以同时取多个别名--> <bean id="user" class="com.ckm.pojo.User" name="user1,user2,user3,user4"/>
import
<!--这个import,一般用于团队开发,他可以将多个配置文件导入合并成一个--> <import resource="beans1.xml"/> <import resource="beans2.xml"/>
总结
本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注猪先飞的更对内容!
相关文章
- 这篇文章主要介绍了如何利用java语言实现经典《复杂迷宫》游戏,文中采用了swing技术进行了界面化处理,感兴趣的小伙伴可以动手试一试...2022-02-01
java 运行报错has been compiled by a more recent version of the Java Runtime
java 运行报错has been compiled by a more recent version of the Java Runtime (class file version 54.0)...2021-04-01- 这篇文章主要介绍了在java中获取List集合中最大的日期时间操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-08-15
- 这篇文章主要介绍了Spring AOP 对象内部方法间的嵌套调用方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-08-29
Spring Cloud 中@FeignClient注解中的contextId属性详解
这篇文章主要介绍了Spring Cloud 中@FeignClient注解中的contextId属性详解,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-09-25Springboot如何实现Web系统License授权认证
这篇文章主要介绍了Springboot如何实现Web系统License授权认证,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-05-28- 这篇文章主要介绍了教你怎么用Java获取国家法定节假日,文中有非常详细的代码示例,对正在学习java的小伙伴们有非常好的帮助,需要的朋友可以参考下...2021-04-23
- 这篇文章主要介绍了Java如何发起http请求的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-03-31
- 说起C#和Java这两门语言(语法,数据类型 等),个人以为,大概有90%以上的相似,甚至可以认为几乎一样。但是在工作中,我也发现了一些细微的差别...2020-06-25
- 这篇文章主要介绍了解决Java处理HTTP请求超时的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-29
如何在Spring WebFlux的任何地方获取Request对象
这篇文章主要介绍了如何在Spring WebFlux的任何地方获取Request对象,帮助大家更好的理解和使用springboot框架,感兴趣的朋友可以了解下...2021-01-26- 这篇文章主要介绍了java 判断两个时间段是否重叠的案例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-08-15
- 这篇文章主要介绍了详解SpringCloudGateway内存泄漏问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-07-16
- 这篇文章主要介绍了超简洁java实现双色球若干注随机号码生成(实例代码),本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-04-02
- 这篇文章主要介绍了Java生成随机姓名、性别和年龄的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-10-01
java 画pdf用itext调整表格宽度、自定义各个列宽的方法
这篇文章主要介绍了java 画pdf用itext调整表格宽度、自定义各个列宽的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-01-31- 这篇文章主要介绍了java正则表达式判断前端参数修改表中另一个字段的值,需要的朋友可以参考下...2021-05-07
Java使用ScriptEngine动态执行代码(附Java几种动态执行代码比较)
这篇文章主要介绍了Java使用ScriptEngine动态执行代码,并且分享Java几种动态执行代码比较,需要的朋友可以参考下...2021-04-15- 这篇文章主要介绍了Java开发实现人机猜拳游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-08-03
- @Autowired 注解的主要功能就是完成自动注入,使用也非常简单,但这篇文章主要给大家介绍了关于Spring为什么不推荐使用@Autowired注解的相关资料,需要的朋友可以参考下...2021-11-03