Spring依赖注入的方式、类型、Bean的作用域、自动注入、在Spring配置文件中引入属性文件
1.Spring依赖注入的方式
- 通过set方法完成依赖注入
- 通过构造方法完成依赖注入
2.依赖注入的类型
- 基本数据类型和字符串 使用value属性
- 如果是指向另一个对象的引入 使用ref属性
User类
package com.alibaba.wlq.bean;
public class User {
private String name;
private Integer age;
private String phone;
private Student student;
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public User(String name, String phone) {
super();
this.name = name;
this.phone = phone;
System.out.println("调用两个参的构造函数");
}
public User(String name, Integer age, String phone) {
super();
this.name = name;
this.age = age;
this.phone = phone;
}
public User() {
super();
System.out.println("调用无参的构造函数");
}
@Override
public String toString() {
return "User [name=" + name + ", age=" + age + ", phone=" + phone + ", student=" + student + "]";
}
public void show() {
System.out.println("调用了show方法");
}
}
Student类
package com.alibaba.wlq.bean;
public class Student {
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Student [address=" + address + "]";
}
}
配置文件
<bean id="stu" class="com.alibaba.wlq.bean.Student">
<property name="address" value="南京"></property>
</bean>
<bean id="user" class="com.alibaba.wlq.bean.User">
<property name="name" value="刘亦菲"></property>
<property name="age" value="18"></property>
<property name="phone" value="10086"></property>
<property name="student" ref="stu"></property>
</bean>
测试类
package com.alibaba.wlq.test;
import org.omg.CORBA.portable.ApplicationException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.alibaba.wlq.bean.User;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext app = new ClassPathXmlApplicationContext("app.xml");
User u = (User) app.getBean("user");
u.show();
System.out.println(u);
}
}
- 如果类对象注入的属性类型为List类型
在User类中增加如下代码
private List<String> list;
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
配置文件中User类的配置改变
<bean id="user" class="com.alibaba.wlq.bean.User">
<property name="name" value="刘亦菲"></property>
<property name="age" value="18"></property>
<property name="phone" value="10086"></property>
<property name="student" ref="stu"></property>
<property name="list">
<list>
<value>1</value>
<value>2</value>
<value>3</value>
<value>4</value>
</list>
</property>
</bean>
测试类中间部分
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext app = new ClassPathXmlApplicationContext("app.xml");
User u = (User) app.getBean("user");
List<String> list = u.getList();
for (String string : list) {
System.out.println(string);
}
u.show();
System.out.println(u);
}
打印结果
调用无参的构造函数
1
2
3
4
调用了show方法
User [name=刘亦菲, age=18, phone=10086, student=Student [address=南京]]
- 如果类对象注入的属性类型为Map类型
在User类中添加如下代码
private Map<String, String> map;
public Map<String, String> getMap() {
return map;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
配置文件
<bean id="user" class="com.alibaba.wlq.bean.User">
<property name="name" value="刘亦菲"></property>
<property name="age" value="18"></property>
<property name="phone" value="10086"></property>
<property name="student" ref="stu"></property>
<property name="list">
<list>
<value>1</value>
<value>2</value>
<value>3</value>
<value>4</value>
</list>
</property>
<property name="map">
<map>
<entry key="lyf" value="刘亦菲"></entry>
<entry key="jsy" value="江疏影"></entry>
<entry key="lye" value="林允儿"></entry>
</map>
</property>
</bean>
测试类
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext app = new ClassPathXmlApplicationContext("app.xml");
User u = (User) app.getBean("user");
List<String> list = u.getList();
for (String string : list) {
System.out.println(string);
}
Map<String, String> map = u.getMap();
for (String k : map.keySet()) {
System.out.println(k+"===="+map.get(k));
}
u.show();
System.out.println(u);
}
打印结果
调用无参的构造函数
1
2
3
4
lyf====刘亦菲
jsy====江疏影
lye====林允儿
调用了show方法
User [name=刘亦菲, age=18, phone=10086, student=Student [address=南京]]
3.Bean的作用域
Bean的作用域默认为单例模式
Strust2:该框架要求非单例
bean标签中有scope属性,不写默认为单例,scope属性表示Bean的作用域,非单例:scope="prototype"
4.自动注入
Service类
package com.alibaba.wlq.service;
import com.alibaba.wlq.bean.Product;
public class ProductService {
private Product product;
public void fun(){
product.find();
}
public Product getProduct() {
return product;
}
public void setProduct(Product product) {
this.product = product;
}
}
Product类
package com.alibaba.wlq.bean;
public class Product {
private String name;
private int num;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public void find() {
System.out.println("======find=====");
}
@Override
public String toString() {
return "Product [name=" + name + ", num=" + num + "]";
}
}
配置文件
<?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"
default-autowire="byName">
<bean id="pService" class="com.alibaba.wlq.service.ProductService" autowire="default">
</bean>
<bean id="product" class="com.alibaba.wlq.bean.Product">
<property name="name" value="5700xt"></property>
<property name="num" value="2"></property>
</bean>
</beans>
测试代码
package com.alibaba.wlq.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.alibaba.wlq.bean.Product;
import com.alibaba.wlq.service.ProductService;
public class Test2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext app = new ClassPathXmlApplicationContext("app2.xml");
ProductService pService = (ProductService) app.getBean("pService");
pService.fun();
Product product = (Product) app.getBean("product");
System.out.println(product);
}
}
打印结果
======find=====
Product [name=5700xt, num=2]
5.在Spring配置文件中引入属性文件
在resources源文件夹下创建properties文件
p.name=3700x
p.num=99
在配置文件中引用properties文件
<context:property-placeholder location="classpath:my.properties"/>
<!-- 引入属性文件,如果要引入多个那就在后面加, -->
<bean id="pService" class="com.alibaba.wlq.service.ProductService" autowire="default">
</bean>
<bean id="product" class="com.alibaba.wlq.bean.Product">
<!--获取属性文件中的属性-->
<property name="name" value="${p.name}"></property>
<prope1rty name="num" value="${p.num}"></property>
</bean>
Spring依赖注入的方式、类型、Bean的作用域、自动注入、在Spring配置文件中引入属性文件的更多相关文章
- sping配置文件中引入properties文件方式
<!-- 用于引入 jdbc有配置文件参数 可以使用PropertyPlaceholderConfigurer 或者PropertyOverrideConfigurer --> <b ...
- 跟着刚哥学习Spring框架--通过XML方式配置Bean(三)
Spring配置Bean有两种形式(XML和注解) 今天我们学习通过XML方式配置Bean 1. Bean的配置方式 通过全类名(反射)的方式 √ id:标识容器中的bean.id唯一. √ cl ...
- 跟着刚哥学习Spring框架--通过注解方式配置Bean(四)
组件扫描:Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件. 特定组件包括: 1.@Component:基本注解,识别一个受Spring管理的组件 2.@Resposit ...
- SPRING IN ACTION 第4版笔记-第二章WIRING BEANS-008-在Java配置文件中引入xml配置文件@Import、@ImportResource
1. package soundsystem; import org.springframework.beans.factory.annotation.Autowired; public class ...
- 代理设置。 安卓工作室配置用http代理。gradle可能需要这些http代理设置去访问互联网。例如下载依赖。 你想要复制ide的代理配置到这个项目的gradle属性文件吗?
代理设置. 安卓工作室配置用http代理.gradle可能需要这些http代理设置去访问互联网.例如下载依赖. 你想要复制ide的代理配置到这个项目的gradle属性文件吗? 查看更多细节,请参阅开发 ...
- spring之通过注解方式配置Bean(一)
(1)组件扫描:spring能够从classpath下自动扫描.侦测和实例化具有特定注解的组件. (2)特定组件包括: @Component:基本注解,标识一个受spring管理的组件: @Respo ...
- Spring(二)--FactoryBean、bean的后置处理器、数据库连接池、引用外部文件、使用注解配置bean等
实验1:配置通过静态工厂方法创建的bean [通过静态方法提供实例对象,工厂类本身不需要实例化!] 1.创建静态工厂类 public class StaticFactory { private st ...
- Spring中使用属性文件properties的两种方式
实际项目中,通常将可配置的参数放到属性文件中,例如数据库连接信息.redis连接信息等,便于统一管理.然后通过IoC框架spring将其加载到上下文中,使得程序可以直接使用. 创建mysql.prop ...
- SPRING IN ACTION 第4版笔记-第二章WIRING BEANS-008-在XML配置文件中引入JAVA配置文件 <import> 、<bean>
一.在xml中引入xml,用<import> <?xml version="1.0" encoding="UTF-8"?> <be ...
随机推荐
- vue第十六单元(element-ui vue-lazyload 等常用插件)
第十六单元(element-ui vue-lazyload 等常用插件) #课程目标 1.掌握插件的引入方式 2.精通UI框架 3.掌握前端常见的几种效果实现 #知识点 一.elementUI的使用 ...
- 赶紧收藏!Spring MVC 万字长文笔记,我愿奉你为王者笔记!
Spring MVC Spring MVC是目前主流的实现MVC设计模式的企业级开发框架,Spring框架的一个子模块,无需整合Spring,开发起来更加便捷. 什么是MVC设计模式? 将应用程序分为 ...
- 利用基于Go Lang的Hugo配合nginx来打造属于自己的纯静态博客系统
Go lang无疑是目前的当红炸子鸡,极大地提高了后端编程的效率,同时有着极高的性能.借助Go语言我们 可以用同步的方式写出高并发的服务端软件,同时,Go语言也是云原生第一语言,Docker,Kube ...
- 5分钟看懂系列:Python 线程池原理及实现
概述 传统多线程方案会使用"即时创建, 即时销毁"的策略.尽管与创建进程相比,创建线程的时间已经大大的缩短,但是如果提交给线程的任务是执行时间较短,而且执行次数极其频繁,那么服务器 ...
- 用matlab提取jpg曲线数据或者jpg图片重新复原
I = imread('111.jpg');%读取处理好的图片,必须是严格坐标轴线为边界的图片 I=rgb2gray(I); %灰度变化 I(I>200)=255; %二值化 I(I<=2 ...
- Error:(18) error: '#FFFF782' is incompatible with attribute android:endColor (attr) color. --Android
android studio 编译是报如下错误: Error:(18) error: '#FFFF782' is incompatible with attribute android:endCol ...
- [论文分享] DHP: Differentiable Meta Pruning via HyperNetworks
[论文分享] DHP: Differentiable Meta Pruning via HyperNetworks authors: Yawei Li1, Shuhang Gu, etc. comme ...
- 7. JDK拍了拍你:字符串拼接一定记得用MessageFormat#format
目录 ✍前言 版本约定 ✍正文 DateFormat:日期时间格式化 SimpleDateFormat NumberFormat:数字格式化 DecimalFormat 一.0和#的使用(最常见使用场 ...
- [leetcode]29. Divide Two Integers不用除法实现除法
思路是不断将被除数分为两部分,每次分的一部分都是尽量大的除数的倍数,然后最后的商就是倍数加上剩下的部分再分,知道不够大. 递归实现 剩下的难点就是,正负号(判断商正负后将两个数都取绝对值),数太大(将 ...
- MongoDb学习(五)---gridfs --http文件下载
现在网上的文章都是用的低版本的jar包,而最新的jar包,下载的方法进行了改变.在网上也没找到好的方法.就用原生的方法进行下载, 我也不知道对不对.反正可以下载了.就先这样吧.后期准备还是用低版本的开 ...