Spring中的依赖注入 spring的依赖注入有哪几种方式

Spring中的依赖注入依赖注入(DI):
依赖注入(DI)是指IC容器在运行期间动态地将某种依赖资源注入到对象中。
例如,将对象B注人(赋值)给对象A的成员变量。
依赖注入的基

依赖注入(DI):

依赖注入(DI)是指IC容器在运行时动态地将某些依赖资源注入到对象中。

例如,将对象B 注入(分配)给对象A 的成员变量。

依赖注入的基本思想是明确定义组件接口,独立开发每个组件,然后根据组件的依赖关系组装和运行组件。

依赖注入的类型:

构造方法注入

com.itheima 包;

公共类用户1 {

私有int ID;

私有字符串名称。

私人字符串密码。

公共User1(int ID, 字符串名称, 字符串密码) {

这个.id=ID;

this.name=名称;

this.password=密码;

}

@覆盖

公共字符串toString() {

返回“用户1{”+

\’id=\’ + ID +

\’, 名称=\’\’ + 名称+ \’\\\\\’\’ +

\’, 密码=\’\’ + 密码+ \’\\\\\’\’ +

\’}\’;

}

}

配置文件

?xml 版本=\’1.0\’ 编码=\’UTF-8\’?

豆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\’

bean id=\’user1\’ class=\’com.itheima.User1\’

构造函数参数名称=\’id\’值=\’1\’/构造函数参数

构造函数参数name=\’name\’ value=\’张三\’/构造函数参数

构造函数参数名称=\’密码\’值=\’123\’/构造函数参数

/豆子

/豆子

测试类

com.itheima 包;

导入org.springframework.context.ApplicationContext。

导入org.springframework.context.support.ClassPathXmlApplicationContext。

公共类测试用户1 {

公共静态无效主(字符串[] args){

ApplicationContext applicationContext=new ClassPathXmlApplicationContext(\’applicationContext-User.xml\’);

User1 user1=applicationContext.getBean(\’user1\’, User1.class);

System.out.println(user1);

}

}

属性 setter 方法注入

com.itheima 包;

公共类用户2 {

私有int ID;

私有字符串名称。

私人字符串密码。

公共无效setId(int id){

这个.id=ID;

}

公共无效setName(字符串名称){

this.name=名称;

}

公共无效setPassword(字符串密码){

this.password=密码;

}

@覆盖

公共字符串toString() {

返回“用户1{”+

\’id=\’ + ID +

\’, 名称=\’\’ + 名称+ \’\\\\\’\’ +

\’, 密码=\’\’ + 密码+ \’\\\\\’\’ +

\’}\’;

}

}

配置文件

?xml 版本=\’1.0\’ 编码=\’UTF-8\’?

豆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\’

bean id=\’user2\’ class=\’com.itheima.User2\’

属性名称=\’id\’ 值=\’2\’/

属性name=\’名称\’ 值=\’李四\’/

属性名称=\’密码\’值=\’456\’/

/豆子

/豆子

测试方法

com.itheima 包;

导入org.springframework.context.ApplicationContext。

导入org.springframework.context.support.ClassPathXmlApplicationContext。

公共类测试用户2 {

公共静态无效主(字符串[] args){

ApplicationContext applicationContext=new ClassPathXmlApplicationContext(\’applicationContext-User2.xml\’);

用户2 user2=applicationContext.getBean(\’user2\’, User2.class);

System.out.println(user2);

}

}

下面是依赖注入的示例实现。

使用依赖注入实现登陆验证

编写Service层

创建UserService 接口

com.itheima.service 包;

//业务逻辑层接口

公共接口用户服务{

公共布尔登录(字符串名称,字符串密码);

}

创建UserService接口的实现类UserServiceImpl

包com.itheima.service.impl;

//导入com.itheima.service.UserService。

导入com.itheima.service.UserService。

导入org.springframework.jdbc.core.JdbcTemplate。

公共类UserServiceImpl 实现UserService {

私有JdbcTemplate jdbcTemplate;

@覆盖

公共布尔登录(字符串用户名,字符串密码){

String sql=\’从用户中选择计数(*),其中用户名=AND 密码=?\’;

整数计数=jdbcTemplate.queryForObject(sql, new Object[]{用户名, 密码}, Integer.class);

返回计数!=null 计数0;

}

//该Setter方法用于注入JdbcTemplate实例。

公共无效setJdbcTemplate(JdbcTemplate jdbcTemplate){

this.jdbcTemplate=jdbcTemplate;

}

}

jdbcTemplate.queryForObject:要运行SQL 查询,请使用JdbcTemplate 的queryForObject 方法。

sql:第一个参数是要执行的SQL查询字符串。 new Object[]{用户名, 密码}:第二个参数是一个对象数组,其中包含要绑定到SQL 查询的参数值(用户名和密码)。 Integer.class:第三个参数是返回结果的类型,表示期望结果是整数。

该方法使用JdbcTemplate来简化数据库操作,防止占位符SQL注入攻击,同时避免了手动管理数据库连接和结果集的繁琐步骤。

关于SQL注入攻击:

public boolean findAdmin(管理员admin)

{

String sql=\’select count(*) from admin where username=\’\’ + admin.getUsername() + “andpassword=\’\’ + admin.getPassword() + \’\’\’ //SQL查询语句

尝试{

结果集res=this.conn.createStatement().executeQuery(sql);

//执行SQL语句

if(res.next())

{

int i=res.getInt(1);//获取第一列的值

如果(i0)

{return true;} //如果结果大于0则返回true

}

} catch (异常e) {

e.printStackTree();//输出异常信息

}

返回假。

}

正常情况下,对于用户username=\’test\’、password=\’test\’,会执行sql语句select count() from admin where username=\’test\’和password=\’test\’。

但如果输入username=\’\’or 1=1–\’,则Java 程序中的String 类型变量sql 将为\’select count() from admin where username=\’ \’or 1=1–\’,password=\’ \’ 执行此SQL 语句时,\’–\’ 会注释掉\’and\’ 后面的语句。这会从username=\’ 或1=1 和1=1 中进行选择,相当于运行count(*),它始终为true,所以运行这条语句的结果实际上是admin表中的行数,而不是与输入的用户名和密码匹配的行数,验证成功。

编写applicationContext.xml配置文件

?xml 版本=\’1.0\’ 编码=\’UTF-8\’?

豆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\’

!– 配置JDBC数据源–

bean id=\’dataSource\’ class=\’org.springframework.jdbc.datasource.DriverManagerDataSource\’

属性名称=\’driverClassName\’值=\’com.mysql.cj.jdbc.Driver\’/

属性名称=\’url\’值=\’jdbc:mysql://localhost/spring?useUnicode=true

amp;characterEncoding=utf-8

amp;serverTimezone=亚洲/上海

amp;useSSL=false\’/

属性名称=\’用户名\’值=\’bge\’/

属性名称=\’密码\’值=\’123456\’/

/豆子

!– 配置JDBC 模板–

Bean ID=\’JdbcTemplate\’

类=\’org.springframework.jdbc.core.JdbcTemplate\’

!– 应使用默认数据源–

属性名称=\’数据源\’ref=\’数据源\’/

/豆子

!– userService bean 由UserServiceImpl 类组成。

然后指定将通过属性标记注入的jdbcTemplate bean。

bean id=\’userService\’ class=\’com.itheima.service.impl.UserServiceImpl\’

属性名称=\’jdbcTemplate\’ ref=\’JdbcTemplate\’/

/豆子

/豆子

编写测试类

com.itheima 包;

导入com.itheima.service.UserService。

导入org.springframework.context.ApplicationContext。

导入org.springframework.context.support.ClassPathXmlApplicationContext。

导入java.util.Scanner。

公共类TestSpring {

公共静态无效主(字符串[] args){

ApplicationContext applicationContext=new ClassPathXmlApplicationContext(\’applicationContext.xml\’);

UserService userService=(UserService) applicationContext.getBean(\’userService\’);

扫描仪扫描仪=新扫描仪(System.in);

System.out.println(\’请输入登录的用户帐号\’);

字符串用户名=Scanner.nextLine();

System.out.println(\’请输入您的密码\’);

字符串密码=scanner.nextLine();

布尔标志=userService.login(用户名, 密码);

如果(标志)

System.out.println(\’登录成功\’);

除此之外

System.out.println(\’登录失败\’);

}

}

依赖注入的工作原理

启动Spring容器:Spring容器根据配置文件**applicationContext.xml**创建并初始化所有bean。创建一个dataSource bean:Spring 容器首先创建并初始化一个**dataSource** bean。创建jdbcTemplate bean: 接下来,Spring 容器创建并初始化**jdbcTemplate** bean,并将**dataSource** bean 注入**jdbcTemplate** bean。创建userService bean:Spring 容器创建并初始化一个**userService** bean(一个**UserServiceImpl** 实例)。设置器注入:

Spring 容器识别**property name=\’jdbcTemplate\’ ref=\’jdbcTemplate\’/**。容器调用**UserServiceImpl 的setJdbcTemplate 方法,并向其传递jdbcTemplate** bean 实例。

当Spring容器创建UserServiceImpl实例时,会自动调用setJdbcTemplate方法,将JdbcTemplate实例注入到UserServiceImpl中进行依赖注入。

总结

依赖注入(DI)和控制反转(IoC)从不同的角度描述同一件事。从应用程序的角度解释依赖注入。也就是说,应用程序依赖IoC 容器来创建和注入必要的外部资源。控制反转也用IoC 容器或IoC 来解释。容器控制应用程序。 IoC 容器则相反。 将应用程序所需的外部资源注入到您的应用程序中。这里所说的外部资源包括外部实例对象、外部文件对象等。

以上#Spring依赖注入相关内容来源仅供参考。相关信息请参见官方公告。

原创文章,作者:CSDN,如若转载,请注明出处:https://www.sudun.com/ask/91369.html

(0)
CSDN的头像CSDN
上一篇 2024年6月21日
下一篇 2024年6月21日

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注