初识 Spring(03)---(XML注入方式 / 注入类型)

简介: XML注入方式1.set 方式注入  2.构造方式注入  3.工厂方式注入set 方式注入1.ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.

XML注入方式

1.set 方式注入  2.构造方式注入  3.工厂方式注入

set 方式注入

1.ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");

2.public class UserDaoImpl implements UserDao;

3.private UserDao dao;

ClassPathXmlApplicationContext 创建了一个 spring 容器后,产生  UserDao 和 dao  对象,并且将 UserDao对象 设置到 dao 对象的属性中,这是通过 set 方法设置的

文件目录:

代码:配置文件:applicationContext.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 http://www.springframework.org/schema/beans/spring-beans.xsd">
	
	
	<bean id="userDao" class="com.neuedu.dao.impl.UserDaoImpl"></bean>
	<bean id="userService" class="com.neuedu.service.UserService">
	<!-- set方法注入 -->
		<property name="dao" ref="userDao"></property>
	</bean>
</beans>
 
AI 代码解读

Test.java

package com.neuedu.test;
import com.neuedu.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {

	public static void main(String[] args) {
		
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		//UserService us= (UserService)ac.getBean("userService");
		
	}
 
}
AI 代码解读

UserDaoImpl.java

package com.neuedu.dao.impl;
import com.neuedu.dao.UserDao;
public class UserDaoImpl implements UserDao{
	public UserDaoImpl() {
		System.out.println("UserDaoImpl构造方法...");
	}
	
	public void save() {
		System.out.println("通过oracle数据库将用户信息保存到数据库中");
	}
	
	
}
AI 代码解读

UserService.java

package com.neuedu.service;
import com.neuedu.dao.UserDao;
import com.neuedu.dao.impl.UserDaoImpl;
public class UserService {
	
	public UserService() {
		System.out.println("UserService 构造方法..."); 
	}
	private UserDao dao;
	
	public void save() {
		dao.save();
		
		
		
	}

	public void setDao(UserDao dao) {
		System.out.println("setUserDao...");
		this.dao = dao;
	}
}
 
AI 代码解读

UserDao.java

package com.neuedu.dao;

public interface UserDao {
	 public void save();
}
AI 代码解读

输出:

构造器方式注入(必须有带参数的构造方法才可以)

代码:配置文件:applicationContext.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 http://www.springframework.org/schema/beans/spring-beans.xsd">
	
	
	<bean id="userDao" class="com.neuedu.dao.impl.UserDaoImpl"></bean>
	<bean id="userService" class="com.neuedu.service.UserService">
	<!-- set方法注入 -->
		<!-- <property name="dao" ref="userDao"></property> -->
		<!-- 构造方法注入 -->
		<constructor-arg ref="userDao"></constructor-arg>
	</bean>
</beans>
 
AI 代码解读

UserService.java

package com.neuedu.service;
import com.neuedu.dao.UserDao;
import com.neuedu.dao.impl.UserDaoImpl;
public class UserService {
	private UserDao dao;
	public UserService() {
		System.out.println("UserService 构造方法..."); 
	}
	public UserService(UserDao dao) {
		System.out.println("带参数的UserService");
		this.dao = dao;
	}	
	public void save() {
		dao.save();	
	}
	public void setDao(UserDao dao) {
		System.out.println("setUserDao...");
		this.dao = dao;
	}
}
 
AI 代码解读

输出:

XML注入类型

1.简单属性  2. 对象  3.集合

简单属性

文件目录:

代码:配置文件:applicationContext.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 http://www.springframework.org/schema/beans/spring-beans.xsd">
	
	
	<bean id="userDao" class="com.neuedu.dao.impl.UserDaoImpl"></bean>
	<bean id="userService" class="com.neuedu.service.UserService">
	<!-- set方法注入 -->
		<!-- <property name="dao" ref="userDao"></property> -->
		<!-- 构造方法注入 -->
		<constructor-arg ref="userDao"></constructor-arg>
	</bean>
	<bean id="person" class="com.neudeu.po.Person">
		<property name="name" value="zhang"></property>
		<property name="age" value="30"></property>
	</bean>
</beans>
 
AI 代码解读

Test.java

package com.neuedu.test;
import com.neudeu.po.Person;
import com.neuedu.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {

	public static void main(String[] args) {
		
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		//UserService us= (UserService)ac.getBean("userService");
		Person person = ac.getBean(Person.class);
		System.out.println(person);
	}
 
}
AI 代码解读

Person.java

package com.neudeu.po;

public class Person {
	private String name;
	private int age;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	@Override
	public String toString() {
		return "Person [name=" + name + ", age=" + age + "]";
	}
}
AI 代码解读

输出:

集合

文件目录:

代码:配置文件:applicationContext.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 http://www.springframework.org/schema/beans/spring-beans.xsd">
	
	
	<bean id="userDao" class="com.neuedu.dao.impl.UserDaoImpl"></bean>
	<bean id="userService" class="com.neuedu.service.UserService">
	<!-- set方法注入 -->
		<!-- <property name="dao" ref="userDao"></property> -->
		<!-- 构造方法注入 -->
		<constructor-arg ref="userDao"></constructor-arg>
	</bean>
	<bean id="FLL" class="com.neudeu.po.Car">
		<property name="brand" value="法拉利"></property>
		<property name="price" value="3000000"></property>
	</bean>
	
	<bean id="BW" class="com.neudeu.po.Car">
		<property name="brand" value="宝马"></property>
		<property name="price" value="300000"></property>
	</bean>
	
	<bean id="BYD" class="com.neudeu.po.Car">
		<property name="brand" value="比亚迪"></property>
		<property name="price" value="30000"></property>
	</bean>
	
	<bean id="person" class="com.neudeu.po.Person">
		<property name="name" value="zhang"></property>
		<property name="age" value="30"></property>
		<property name="cars">
			<list>
				<ref bean="FLL"/>
				<ref bean="BW"/>
				<ref bean="BYD"/>
			</list>
		</property>
	</bean>
</beans>
 
AI 代码解读

Person.java

package com.neudeu.po;

import java.util.List;

public class Person {
	private String name;
	private int age;
	
	private List<Car> cars;
	
	public List<Car> getCars() {
		return cars;
	}
	public void setCars(List<Car> cars) {
		this.cars = cars;
	}
	@Override
	public String toString() {
		return "Person [name=" + name + ", age=" + age + ", cars=" + cars + "]";
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	
	
}
AI 代码解读

Car.java

package com.neudeu.po;

public class Car {
	private String brand;
	private double price;
	public String getBrand() {
		return brand;
	}
	public void setBrand(String brand) {
		this.brand = brand;
	}
	public double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	@Override
	public String toString() {
		return "Car [brand=" + brand + ", price=" + price + "]";
	}
	
	
}
AI 代码解读

输出:

代码:配置文件:applicationContext.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 http://www.springframework.org/schema/beans/spring-beans.xsd">
	
	
	<bean id="userDao" class="com.neuedu.dao.impl.UserDaoImpl"></bean>
	<bean id="userService" class="com.neuedu.service.UserService">
	<!-- set方法注入 -->
		<!-- <property name="dao" ref="userDao"></property> -->
		<!-- 构造方法注入 -->
		<!-- <constructor-arg ref="userDao"></constructor-arg> -->
		<property name="dao">
			<bean class="com.neuedu.dao.impl.UserDaoImpl"></bean>
		</property>
	</bean>
	<bean id="FLL" class="com.neudeu.po.Car">
		<property name="brand" value="法拉利"></property>
		<property name="price" value="3000000"></property>
	</bean>
	
	<bean id="BW" class="com.neudeu.po.Car">
		<property name="brand" value="宝马"></property>
		<property name="price" value="300000"></property>
	</bean>
	
	<bean id="BYD" class="com.neudeu.po.Car">
		<property name="brand" value="比亚迪"></property>
		<property name="price" value="30000"></property>
	</bean>
	
	<bean id="person" class="com.neudeu.po.Person">
		<property name="name" value="zhang"></property>
		<property name="age" value="30"></property>
		<property name="cars">
			<list>
				<bean class="com.neudeu.po.Car">
					<property name="brand" value="红旗"></property>
					<property name="price" value="300000"></property>
				</bean>
				<ref bean="FLL"/>
				<ref bean="BW"/>
				<ref bean="BYD"/>
			</list>
		</property>
	</bean>
</beans>
 
AI 代码解读

Test.java

package com.neuedu.test;
import com.neudeu.po.Person;
import com.neuedu.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {

	public static void main(String[] args) {
		
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		
	}
 
}
AI 代码解读

输出:

目录
打赏
0
0
0
0
2
分享
相关文章
微服务——SpringBoot使用归纳——Spring Boot使用slf4j进行日志记录—— logback.xml 配置文件解析
本文解析了 `logback.xml` 配置文件的详细内容,包括日志输出格式、存储路径、控制台输出及日志级别等关键配置。通过定义 `LOG_PATTERN` 和 `FILE_PATH`,设置日志格式与存储路径;利用 `&lt;appender&gt;` 节点配置控制台和文件输出,支持日志滚动策略(如文件大小限制和保存时长);最后通过 `&lt;logger&gt;` 和 `&lt;root&gt;` 定义日志级别与输出方式。此配置适用于精细化管理日志输出,满足不同场景需求。
44 1
微服务——SpringBoot使用归纳——Spring Boot集成MyBatis——基于 xml 的整合
本教程介绍了基于XML的MyBatis整合方式。首先在`application.yml`中配置XML路径,如`classpath:mapper/*.xml`,然后创建`UserMapper.xml`文件定义SQL映射,包括`resultMap`和查询语句。通过设置`namespace`关联Mapper接口,实现如`getUserByName`的方法。Controller层调用Service完成测试,访问`/getUserByName/{name}`即可返回用户信息。为简化Mapper扫描,推荐在Spring Boot启动类用`@MapperScan`注解指定包路径避免逐个添加`@Mapper`
24 0
在使用Spring的`@Value`注解注入属性值时,有一些特殊字符需要注意
【10月更文挑战第9天】在使用Spring的`@Value`注解注入属性值时,需注意一些特殊字符的正确处理方法,包括空格、引号、反斜杠、新行、制表符、逗号、大括号、$、百分号及其他特殊字符。通过适当包裹或转义,确保这些字符能被正确解析和注入。
362 3
【SpringFramework】Spring IoC-基于XML的实现
本文主要讲解SpringFramework中IoC和DI相关概念,及基于XML的实现方式。
126 69
Spring AOP—通知类型 和 切入点表达式 万字详解(通俗易懂)
Spring 第五节 AOP——切入点表达式 万字详解!
136 25
使用idea中的Live Templates自定义自动生成Spring所需的XML配置文件格式
本文介绍了在使用Spring框架时,如何通过创建`applicationContext.xml`配置文件来管理对象。首先,在resources目录下新建XML配置文件,并通过IDEA自动生成部分配置。为完善配置,特别是添加AOP支持,可以通过IDEA的Live Templates功能自定义XML模板。具体步骤包括:连续按两次Shift搜索Live Templates,配置模板内容,输入特定前缀(如spring)并按Tab键即可快速生成完整的Spring配置文件。这样可以大大提高开发效率,减少重复工作。
使用idea中的Live Templates自定义自动生成Spring所需的XML配置文件格式
一键注入 Spring 成员变量,顺序编程
介绍了一款针对Spring框架开发的插件,旨在解决开发中频繁滚动查找成员变量注入位置的问题。通过一键操作(如Ctrl+1),该插件可自动在类顶部添加`@Autowired`注解及其成员变量声明,同时保持光标位置不变,有效提升开发效率和代码编写流畅度。适用于IntelliJ IDEA 2023及以上版本。
一键注入 Spring 成员变量,顺序编程
Spring容器Bean之XML配置方式
通过对以上内容的掌握,开发人员可以灵活地使用Spring的XML配置方式来管理应用程序的Bean,提高代码的模块化和可维护性。
93 6
Spring高手之路24——事务类型及传播行为实战指南
本篇文章深入探讨了Spring中的事务管理,特别是事务传播行为(如REQUIRES_NEW和NESTED)的应用与区别。通过详实的示例和优化的时序图,全面解析如何在实际项目中使用这些高级事务控制技巧,以提升开发者的Spring事务管理能力。
97 1
Spring高手之路24——事务类型及传播行为实战指南
数据集学习笔记(二): 转换不同类型的数据集用于模型训练(XML、VOC、YOLO、COCO、JSON、PNG)
本文详细介绍了不同数据集格式之间的转换方法,包括YOLO、VOC、COCO、JSON、TXT和PNG等格式,以及如何可视化验证数据集。
1025 1
数据集学习笔记(二): 转换不同类型的数据集用于模型训练(XML、VOC、YOLO、COCO、JSON、PNG)

热门文章

最新文章

AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等