spring学习日志二
一、spring依赖注入的方式
1.通过set方法来完成注入
<bean id="student" class="com.zhiyou100.xz.spring.Student" >
<!-- property:通过set方法来注入Student类 的name属性值-->
<property name="name" value="李四"/>
<property name="age" value="18"/>
</bean>
2.通过构造方法来完成依赖注入
<bean id="student2" class="com.zhiyou100.xz.spring.Student">
<!--
constructor-arg:构造方法的参数
index:第几个参数 索引从0开始
只有一个参数时,即当index="0"时value的值先默认的是string类型,要用name区分不同的参数名
name:通过构造方法的参数名
-->
<!-- 当一个构造方法有两个参数时,要用两个constructor-arg表示 -->
<constructor-arg index="0" value="里斯"/>
<constructor-arg index="1" value="18"/>
</bean>
测试
public class Test {
public static void main(String[] args) {
//加载spring配置文件
ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");
//获取指定的类对象
Student s=(Student) app.getBean("student2");
//s.show();
System.out.println(s.getName());
System.out.println(s.getAge());
}
}
二、依赖注入的数据类型
1.基本数据类型和字符串使用value
<bean id="address" class="com.zhiyou100.xz.spring.Address">
<property name="address" value="南京"></property>
</bean>
2.如果是指向另一个对象的引用,使用ref
//该类需要由spring容器来管理
public class Student {
private String name;//该类中属性的注入也由spring来注入
private int age;
private Address ad;//该属性是一个对象
}
<bean id="student" class="com.zhiyou100.xz.spring.Student" >
<!-- property:通过set方法来注入Student类 的name属性值-->
<property name="name" value="李四"/>
<property name="age" value="18"/>
<!-- ref指向另一个bean对象 -->
<property name="ad" ref="address"/>
</bean>
<bean id="address" class="com.zhiyou100.xz.spring.Address">
<property name="address" value="南京"></property>
</bean>
3.如果类对象注入的属性类型为list类型
//该类需要由spring容器来管理
public class Student {
private String name;//该类中属性的注入也由spring来注入
private int age;
private Address ad;//该属性是一个对象
private List<Address> list;
public List<Address> getList() {
return list;
}
public void setList(List<Address> list) {
this.list = list;
}
}
<bean id="student" class="com.zhiyou100.xz.spring.Student" >
<!-- property:通过set方法来注入Student类 的name属性值-->
<property name="name" value="李四"/>
<property name="age" value="18"/>
<!-- ref指向另一个bean对象 -->
<property name="ad" ref="address"/>
<property name="list">
<list>
<bean class="com.zhiyou100.xz.spring.Address">
<property name="address" value="南京"></property>
</bean>
<bean class="com.zhiyou100.xz.spring.Address">
<property name="address" value="北京"></property>
</bean>
<bean class="com.zhiyou100.xz.spring.Address">
<property name="address" value="西京"></property>
</bean>
</list>
</property>
</bean>
4.如果类对象注入的属性类型为map类型
public class Student {
private String name;//该类中属性的注入也由spring来注入
private int age;
private Address ad;//该属性是一个对象
private List<Address> list;
private Map<String, String> map;
public Map<String, String> getMap() {
return map;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
}
<bean id="student" class="com.zhiyou100.xz.spring.Student" scope="prototype">
<!-- property:通过set方法来注入Student类 的name属性值-->
<property name="name" value="李四"/>
<property name="age" value="18"/>
<property name="map">
<map>
<entry key="lisi" value="里斯"/>
<entry key="kd" value="恐蛋"/>
<entry key="dn" value="达尼"/>
</map>
</property>
</bean>
三、Bean的作用域
Bean的作用域默认为单例模式
<!--
scope:表示bean的作用域 默认为单例singleton
prototype:原生。非单例 struts2:该框架要求非单例
-->
<bean id="student" class="com.zhiyou100.xz.spring.Student" scope="prototype">
<!-- property:通过set方法来注入Student类 的name属性值-->
<property name="name" value="李四"/>
<property name="age" value="18"/> </bean>
测试
public class Test {
public static void main(String[] args) {
//加载spring配置文件
ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");
//获取指定的类对象
Student s=(Student) app.getBean("student");
Student s1=(Student) app.getBean("student");
//s与s1的引用地址不同
}
}
四、自动注入
<?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="userDao" class="com.zhiyou100.xz.spring.UserDao"></bean>
<!-- <bean id="udao" class="com.zhiyou100.xz.spring.UserDao"></bean> -->
<!-- autowire:自动注入的属性
byType:根据userDao属性的类型,找到与之匹配的bean
private UserDao userDao;
byName:根据属性名找与之匹配的bean的id
no:需要手动注入
default:采取全局的default-autowire设置
-->
<bean id="uService" class="com.zhiyou100.xz.spring.UserService" autowire="default">
<!-- <property name="userDao" ref="udao"></property> --> </bean> </beans>
五、在spring配置文件中引入属性文件
创建User.java
public class User {
private String name;
private int age;
private String address;
}
创建UserService.java
public class UserService {
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
创建属性文件my.properties
user.names=xz
user.age=18
user.address=\u5357\u4EAC
创建app.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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 引入属性文件2.5以后就有了
如果引入多个文件可以使用通配符*,也可以使用逗号分割引入
location="classpath:*.properties"
location="classpath:my*.properties"
location="classpath:my.properties,classpath:a.properties"
-->
<!-- 加载属性文件用context:property-placeholder -->
<context:property-placeholder location="classpath:my.properties"/>
<bean id="users" class="com.zhiyou100.xz.spring.User">
<property name="name" value="${user.names}"></property>
<property name="age" value="${user.age}"></property>
<property name="address" value="${user.address}"></property>
</bean> <bean id="uService" class="com.zhiyou100.xz.spring.UserService" >
<property name="user" ref="users"></property> </bean>
</beans>
测试
public class Test2 {
public static void main(String[] args) {
ApplicationContext app=new ClassPathXmlApplicationContext("app.xml");
UserService us=(UserService) app.getBean("uService");
System.out.println(us.getUser());
}
}
六、使用注解的方式
1.引入jar包:

2.配置文件中使用包扫描
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd"
>
<!-- 1. 包扫描:扫描注解所在的包 component-scan:部件扫描-->
<context:component-scan base-package="com.zhiyou100.xz"></context:component-scan> </beans>
3.在相应的类中加上注解
@Repository 持久化注解。
@Service 业务层注解
@Controller 控制层注解
@Autowired 自动注入 按照类型帮你自动注入,如果由多个类型相同的那么就会在按照名称注入。(建议使用这个)
@Resouce 自动注入 按照名称注入,如果没有相同名称的bean那么会按照类型帮你注入。 它可以指定名称来注入。
controller的代码
@Controller(value="userController")
public class UserController { @Autowired
private UserService userService; public void setUserService(UserService userService) {
this.userService = userService;
}
public String findById(int id) {
userService.queryById(id);
return "update";
}
}
service的代码
public interface UserService {
public void queryById(int id);
}
@Service(value="userService")
public class UserServiceImp implements UserService{
//@Autowired //注入了UserDao接口的实现类。按照类型注入<property name="userDao" ref="userDao"/>
@Resource(name="userDao") //按照名称注入
private UserDao userDao;//依赖注入UserDao接口的实现类对象 public void setUserDao(UserDao userDao) { //set方法可省略
this.userDao = userDao;
} @Override
public void queryById(int id) {
userDao.selectById(id); } }
dao的代码
public interface UserDao {
public void selectById(int id);
}
@Repository(value="userDao")
public class UserDaoImp implements UserDao{ @Override
public void selectById(int id) {
System.out.println("根据id查询数据库信息2");
}
}
spring学习日志二的更多相关文章
- spring 学习(二):spring bean 管理--配置文件和注解混合使用
spring 学习(二)spring bean 管理--配置文件和注解混合使用 相似的,创建 maven 工程,配置pom.xml 文件,具体可以参考上一篇博文: sprint 学习(一) 然后我们在 ...
- Spring学习(二)——Spring中的AOP的初步理解[转]
[前面的话] Spring对我太重要了,做个关于web相关的项目都要使用Spring,每次去看Spring相关的知识,总是感觉一知半解,没有很好的系统去学习一下,现在抽点时间学习一下Spring. ...
- Spring学习(二)——Spring中的AOP的初步理解
[前面的话] Spring对我太重要了,做个关于web相关的项目都要使用Spring,每次去看Spring相关的知识,总是感觉一知半解,没有很好的系统去学习一下,现在抽点时间学习一下Spring.不知 ...
- spring学习总结二-----面向切面编程(AOP)思想
上一篇spring博客简总结了spring控制反转和依赖注入的相关思想知识点,这篇博文对spring的面向切的编程思想进行简单的梳理和总结. 一.面向切面的思想 与面向对象的纵向关系概念不同,面向切面 ...
- Spring学习笔记(二)之装配Bean
一,介绍Bean的装配机制 在Spring中,容器负责对象的创建并通过DI来协调对象之间的关系.但是我们要告诉Spring创建哪些Bean并且如何将其装配在一起.,装配wiring就是DI依赖注入的本 ...
- spring学习日志三
一.回顾 1.1 依赖注入的方式. set方法来注入 <property name="属性名" /> 构造方法来注入<construtor-arg index=& ...
- Spring学习(二)
1. AOP的思想(如何实现),AOP在哪些地方使用? 相关术语有哪些? AOP是面向切面编程,它是一种编程思想,采取横向抽取机制,取代了传统纵向继承体系重复性代码的方式 应用场景有: 记录日志 监控 ...
- Spring学习记录(二)---容器和bean属性配置
下载spring包,在eclipse搭建spring环境. 这步我在eclipse中无法导入包,看网上的: http://sishuok.(和谐)com/forum/blogPost/list/242 ...
- Spring学习总结二——SpringIOC容器二
一:指定bean的依赖关系 例如examplebean对象依赖examplebean1对象,那么在创建examplebean对象之前就 需要先创建examplebean1对象. 1:创建Example ...
随机推荐
- C语言:位运算符总结
位运算符:1.指对操作数以二进制位( bit)为单位进行的数据处理2.每一个二进制位只存放0或13. 取反:~ 按位反 ~ 0变1 1变0 ~1=0 ~0=14.异或: ^ 相同为0,不相同为1 1 ...
- 双线性插值算法的FPGA实现
本设计预实现720P到1080P的图像放大,输入是YUV444数据,分量像素位宽为10bit,采用的算法为双线性插值法,开发平台是xiinx K7开发板. 双线性插值法即双次线性插值,首先在横向线性插 ...
- 10分钟系列:NetCore3.1+EFCore三步快速完成数据库交互
前言 做程序开发,不管是什么语言什么数据库,其中的ORM(对象关系映射)是必不可少的,但是不管选择哪一种ORM,都需要了解其中的运行机制,配置帮助类等等. 所以很多ORM都开始进行升级封装,我们只需要 ...
- 微信小程序云开发-云存储-带图片的商品列表携带id跳转至商品详情
一.商品列表页 1.wxml文件 在view中添加点击事件goToGoodDetail,绑定数据data-id <!-- 添加点击事件goToGoodDetail --> <view ...
- 【LOJ 109 并查集】 并查集
题目描述 这是一道模板题. 维护一个 n 点的无向图,支持: 加入一条连接 u 和 v 的无向边 查询 u 和 v 的连通性 由于本题数据较大,因此输出的时候采用特殊的输出方式:用 0 或 1 代表每 ...
- java String转List<Device>集合
// 从Redis中获得正常设备的数量 String success = redisService.get(RedisKey.CULTIVATION_RECORD_SUCCESS); //建立一个li ...
- sql注入之类型及提交注入
#参数类型 这里说的参数是源码中存在注入的地方. 其中参数类型有:数字.字符.搜索.json等. 其中sql语句干扰符号有:',",%,),}等,过滤首先考虑闭合这些符号,再进行注入测试. ...
- Liferay Portal CE 反序列化命令执行漏洞(CVE-2020-7961)
影响范围 Liferay Portal 6.1.X Liferay Portal 6.2.X Liferay Portal 7.0.X Liferay Portal 7.1.X Liferay Por ...
- 论文笔记:(NIPS2018)PointCNN: Convolution On X-Transformed Points
目录 摘要 一.2D卷积应用在点云上存在的问题 二.解决的方法 2.1 idea 2.2 X-conv算子 2.3 分层卷积 三.实验 3.1分类和分割 3.2消融实验.可视化和模型复杂度 总结 仍存 ...
- 【GCC编译器】将GIMPLE序列划分成基本块(Basic block),并构造控制流图
1. 首先介绍测试用例,这是一个简单的if-then-else结构,输入为 int 类型的单变量,输出为 int 类型的结果.如果条件 a < 1 成立,则将输入直接返回:如果条件不成立,则返回 ...