初识 Spring(03)---(XML注入方式 / 注入类型)
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>
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"); } }
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数据库将用户信息保存到数据库中"); } }
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; } }
UserDao.java
package com.neuedu.dao; public interface UserDao { public void save(); }
输出:
构造器方式注入(必须有带参数的构造方法才可以)
代码:配置文件: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>
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; } }
输出:
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>
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); } }
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 + "]"; } }
输出:
集合
文件目录:
代码:配置文件: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>
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; } }
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 + "]"; } }
输出:
代码:配置文件: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>
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"); } }
输出:

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
Java源码阅读之ReentrantLock - lock和unLock方法
阅读优秀的源码是提升编程技巧的重要手段之一。 如有不对的地方,欢迎指正 转载请注明出处https://blog.lzoro.com。 碎碎念 如果需要使用或者了解ReentrantLock,证明已经步入并发编程领域了,这里理论基础不多提,需要的自行查阅资料。 但是,相关术语还是要做一下描述的。 ReentrantLock:可重入锁 AQS:AbstractQueuedSynchronized 抽象类,队列式同步器 CAS:Compare and Swap, 比较并交换值 CLH队列:The wait queue is a variant of a "CLH" (Craig, Landin, and * Hagersten) lock queue. ReentrantLock 首先,贴图大家感受一下。 ReentrantLock Sync 其中Sync是ReentrantLock的抽象静态内部类,提供了锁的同步措施,具体实现有NonFairSync和FairSync,分别为公平和非公平锁。 从图中我们可以看出,ReentrantLock是实现了Lock接口和Serializable接口,...
- 下一篇
初识 Spring(01)---(浅谈 Spring 框架)
浅谈 Spring 框架 Spring框架是分模块存在,除了最核心的Spring Core Container(即Spring容器)是必要模块之外,其他模块都是可选,视需要而定。大约有20多个模块。Spring的核心是控制反转(IoC)和面向切面(AOP)。简单来说,Spring是一个分层的JavaSE/EE full-stack(一站式) 轻量级开源框架。 一般来说,Spring主要分为7个模块: 构建项目 文件目录: 代码: 配置文件: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...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- Docker快速安装Oracle11G,搭建oracle11g学习环境
- SpringBoot2更换Tomcat为Jetty,小型站点的福音
- Docker安装Oracle12C,快速搭建Oracle学习环境
- CentOS关闭SELinux安全模块
- CentOS7,8上快速安装Gitea,搭建Git服务器
- CentOS8安装Docker,最新的服务器搭配容器使用
- SpringBoot2配置默认Tomcat设置,开启更多高级功能
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- Eclipse初始化配置,告别卡顿、闪退、编译时间过长
- Docker使用Oracle官方镜像安装(12C,18C,19C)