console.log|

JayZzz

园龄:2年10个月粉丝:1关注:0

2022-10-25 19:06阅读: 99评论: 0推荐: 0

Spring依赖注入问题

记录一下最近复习的Spirng依赖注入问题
主要介绍两个注入方式 也是用的最多的 1.构造方法注入 2.Setter注入。

参考文档:https://blog.csdn.net/weixin_55418082/article/details/124466712

1.构造方法注入

原理:使用构造方法注入指的时Spring容器调用构造方法注入被依赖的实例(对象),构造方法可以是有参的或者是无参的。当spring读取配置信息后,会通过反射的方式调用实例的构造方法。如果是有参数构造方法,可以在构造方法中注入所需的参数值,最后创建类对象。

具体代码

实体类:

public class User {
private String id;
private String name;
private String password;
//添加有参构造方法
public User(String id, String name, String password) {
this.id = id;
this.name = name;
this.password = password;
//用于判段spring是否使用此方法
System.out.println("通过有参构造方法进行注入");
}
//添加toString()
@Override
public String toString() {
return "User{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", password='" + password + '\'' +
'}';
}
}

配置文件进行有参构造注入属性:

<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
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--创建一个User类的Bean实例-->
<bean id="user1" class="com.DI.User">
<!--进行有参构造赋值-->
<constructor-arg name="id" value="1"/>
<constructor-arg name="name" value="张三"/>
<constructor-arg name="password" value="123"/>
</bean>
</beans>

其中元素是用于给User类的有参构造方法进行赋值操作。Spring通过 User类的构造方法获取元素定义的值,最后这些值会被赋值给Spring创建User对象

测试类

public class userConstrtTest {
public static void main(String[] args) {
//加载配置文件,并且创建该实例
ApplicationContext applicationContext = new
ClassPathXmlApplicationContext("bean1.xml");
//获取创建的对象实例
User user = applicationContext.getBean("user1",User.class);
//输出对象值
System.out.println(user);
}
}

输出:

2.Setter方法注入

Setter方法注入是当今Spring最主流的注入方法,你可以再各种SSM项目中见到它的身影

具体代码实现:

首先是一个实体类

public class User2 {
private String id;
private String name;
private String password;
public void setId(String id) {
this.id = id;
//依旧进行打印跟踪
System.out.println("set方法注入id属性");
}
public void setName(String name) {
this.name = name;
System.out.println("set方法注入name属性");
}
public void setPassword(String password) {
this.password = password;
System.out.println("set方法注入password属性");
}
@Override
public String toString() {
return "User2{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", password='" + password + '\'' +
'}';
}
}

配置文件创建对应的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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--创建一个user bean实例-->
<bean id="user2" class="com.DI.User2">
<!--进行setter方法赋值操作-->
<property name="id" value="2"></property>
<property name="name" value="李明"></property>
<property name="password" value="456"></property>
</bean>
</beans>

从代码中我们可以知道,当我们在使用setter方法进行诸如操作的时候,我们使用的是标签,里面的name是用于指定字段名称,后面的value是用来设定具体值的。

测试类:

public class TestUser2 {
public static void main(String[] args) {
//加载配置文件
ApplicationContext applicationContext =
new ClassPathXmlApplicationContext("bean2.xml");
//获取bean创建的对象
User2 user2 = applicationContext.getBean("user2", User2.class);
System.out.println(user2);
}
}

输出:

成功创建对象实例!.并且通过我设定的路径跟踪信息可以知道这是通过setter方法进行注入操作的

Setter方法注入对象的实际应用操作

//创建一个业务逻辑接口
public interface UserDao {
//定义一个方法用于模拟登录功能的实现
public boolean login(String name,String password);
}

创建该接口的实现类:

import com.DI.dao.UserDao;
//创建一个业务逻辑接口
public class UserDaoImpl implements UserDao {
public boolean login(String name, String password) {
//如果名字是比尔和密码是123456就返回true
if(name.equals("比尔")&&password.equals("12345")){
return true;
}
return false;
}
}

接下来编写服务层接口,userService

//服务层
public interface UserService {
//创建服务层的登录方法
public boolean login(String name,String password);
}

userService的实现类:

import com.DI.Service.UserService;
import com.DI.dao.UserDao;
//创建userservice的实现类
public class UserServiceImpl implements UserService {
//创建一个UserDao类型的类变量
UserDao userDao;
//添加setter方法
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
//重写userService接口中的方法
public boolean login(String name, String password) {
return userDao.login(name,password);
}
}

创建spring配置文件创建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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--创建一个userDao的实例, <!--因为接口不能够实例化建对象,所以只能找他的子类建对象-->-->
<bean id="userDaoImpl" class="com.DI.dao.Impl.UserDaoImpl"></bean>
<!--创建一个UserService实例并将userdao注入到userservice当中-->
<bean id="userService" class="com.DI.Service.Impl.UserServiceImpl">
<property name="userDao" ref="userDaoImpl"></property>
</bean>
</beans>

由上述代码可知,我们先创建userDao接口实现类的bean,然后通过标签中的ref 对userdao进行注入操作。

对于标签进行解释:

name属性值:类里面属性名称

ref属性:创建userDao对象bean标签id值

最后建立一个测试类进行测试操作

import com.DI.Service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class userTYest {
public static void main(String[] args) {
ApplicationContext applicationContext=
new ClassPathXmlApplicationContext("beanUser.xml");
UserService userService =(UserService)applicationContext.getBean( "userService");
boolean flag = userService.login("比尔","12345");
if(flag){
System.out.println("登录成功");
}else{
System.out.println("登录失败");
}
}
}

在这里我们的userService接口用器实现类创建的对象调用login方法进而调用userDao登录逻辑代码进行登录功能的实现,在这可以发现其中的妙处,不仅实现了简化代码还能够最大限度的解耦操作!

本文作者:JayZzzWh

本文链接:https://www.cnblogs.com/JayZzzWh/p/16825977.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   JayZzz  阅读(99)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起
  1. 1 阴天快乐 yihuik苡慧
  2. 2 依然爱你 yihuik苡慧
  3. 3 1 AM thomeboydontkill
  4. 4 我发现你 Fine乐团
  5. 5 胡言乱语 Fine乐团
  6. 6 回答 YOUNG
  7. 7 告诉你我多想你 王子明,杀不死Subs
  8. 8 Busy Body Claire Ridgely
  9. 9 不值得你喜欢 王子明,7copy,WYAN王毓千
  10. 10 Admiration T.Yee,奚缘
  11. 11 Letting Go 刘大拿
  12. 12 Lover Boy 88 Higher Brothers,Phum Viphurit
  13. 13 暴风雨 Higher Brothers,HARIKIRI
  14. 14 Diamond Higher Brothers
1 AM - thomeboydontkill
00:00 / 00:00
An audio error has occurred, player will skip forward in 2 seconds.

作词 : thomeboydontkill

作曲 : thomeboydontkill

编曲 : eeryskies.

制作人 : thomeboydontkill

录音 : CSNV stuido

混音 : 谭玉堂

母带 : 张步若

我并不完美

我需要爱

在我拥有你的时间

我只想你在

不再胡思乱想

自己到底特不特别

甜蜜的话语

OK

我承认

我确实着迷不浅

就像是

大雁盼着春风

我盼着和你见面

有时会控制不住的想你

我早已习惯了等

我想把我的爱

封存在想你的一点整

所有代价我都不计

就当我在发疯

生活带给我的泥泞

有你全被冲刷

你像个童话

出现在我身边

治愈我的痛啊

确实很过分

我想要占有你

你的每一寸肌肤

都把我给迷住

我承认

我想要被你看作做最好的那个

太过在意面子不愿说出口

毋庸置疑我已深陷这其中

你是否和我一样

喜欢你说话的方式

如果有能让时间穿越的装置

我不会希望我们更早的相遇

我会穿越到有我们的未来

一起经历那些所谓的hard times

为了你我不会被现实打败

让你坚信你就是我的最爱

你给我的吻成就了最好7月

你是我的唯一我希望你能理解

只需要相信我 dont give a thought about wat he said

你是我的天使我来成为你的羽翼

有时会控制不住的想你

我早已习惯了等

我想把我的爱

封存在想你的一点整

所有代价我都不计

就当我在发疯

生活带给我的泥泞

有你全被冲刷

你像个童话

Baby,我想要占有你

想要占有你

我想要占有你

我想要占有你

我想要占有

OP:成都菲乐普文化传媒有限公司

SP:亚洲华纳(北京)音乐娱乐咨询有限公司

版权声明:未经著作权人书面许可,任何人不得以任何方式使用(包括翻唱、翻录等形式)