一、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学习日志二的更多相关文章

  1. spring 学习(二):spring bean 管理--配置文件和注解混合使用

    spring 学习(二)spring bean 管理--配置文件和注解混合使用 相似的,创建 maven 工程,配置pom.xml 文件,具体可以参考上一篇博文: sprint 学习(一) 然后我们在 ...

  2. Spring学习(二)——Spring中的AOP的初步理解[转]

      [前面的话] Spring对我太重要了,做个关于web相关的项目都要使用Spring,每次去看Spring相关的知识,总是感觉一知半解,没有很好的系统去学习一下,现在抽点时间学习一下Spring. ...

  3. Spring学习(二)——Spring中的AOP的初步理解

    [前面的话] Spring对我太重要了,做个关于web相关的项目都要使用Spring,每次去看Spring相关的知识,总是感觉一知半解,没有很好的系统去学习一下,现在抽点时间学习一下Spring.不知 ...

  4. spring学习总结二-----面向切面编程(AOP)思想

    上一篇spring博客简总结了spring控制反转和依赖注入的相关思想知识点,这篇博文对spring的面向切的编程思想进行简单的梳理和总结. 一.面向切面的思想 与面向对象的纵向关系概念不同,面向切面 ...

  5. Spring学习笔记(二)之装配Bean

    一,介绍Bean的装配机制 在Spring中,容器负责对象的创建并通过DI来协调对象之间的关系.但是我们要告诉Spring创建哪些Bean并且如何将其装配在一起.,装配wiring就是DI依赖注入的本 ...

  6. spring学习日志三

    一.回顾 1.1 依赖注入的方式. set方法来注入 <property name="属性名" /> 构造方法来注入<construtor-arg index=& ...

  7. Spring学习(二)

    1. AOP的思想(如何实现),AOP在哪些地方使用? 相关术语有哪些? AOP是面向切面编程,它是一种编程思想,采取横向抽取机制,取代了传统纵向继承体系重复性代码的方式 应用场景有: 记录日志 监控 ...

  8. Spring学习记录(二)---容器和bean属性配置

    下载spring包,在eclipse搭建spring环境. 这步我在eclipse中无法导入包,看网上的: http://sishuok.(和谐)com/forum/blogPost/list/242 ...

  9. Spring学习总结二——SpringIOC容器二

    一:指定bean的依赖关系 例如examplebean对象依赖examplebean1对象,那么在创建examplebean对象之前就 需要先创建examplebean1对象. 1:创建Example ...

随机推荐

  1. ADB打开快手APP

    aa="adb -s {0} shell am start -n com.kuaishou.nebula/com.yxcorp.gifshow.HomeActivity".form ...

  2. PyCharm代码区不能编辑的解决办法

    问题: 修改之前的Python代码时发现代码区无法编辑,无意中输入i后又可以编辑了. 解决: 原因是打开了工具中的vim Emulator编辑模式,把vim Emulator前面的勾取消即可.

  3. File类与常用IO流第三章IO流概述

    一:以内存为基准,按照数据的流动方向,流向内存为输入(读取数据),流出内存为输出.IO流有四大顶级父类: IO流四大顶级父类   输入流 输出流 字节流 字节输入流 InputStream 字节输出流 ...

  4. war项目依赖war项目

    还没有看,立个flag:https://my.oschina.net/u/588379/blog/1817077

  5. Leetcode12. 整数转罗马数字Leetcode18. 四数之和

    > 简洁易懂讲清原理,讲不清你来打我~ 输入整数,输出对应的罗马字符串![在这里插入图片描述](https://img-blog.csdnimg.cn/54b001c62a0d4d348c962 ...

  6. springboot-3-web开发

    一.视图层技术thymeleaf 我们一般都是基于3.x版本 1.流程: 导入依赖 <!--整合thymeleaf技术--> <dependency> <groupId& ...

  7. Linux服务系统申请SSL证书方法

    inux主要面向专业性较强的技术人员,如果是WEB站点通常采取PHP语言为主选,可选的服务器环境中有Apache.Nginx.Tomcat这几类为主的框架环境,有的图方便会用一些可视化一键式的控制面板 ...

  8. fiddler抓取手机模拟器数据

    引自:https://blog.csdn.net/lengdaochuqiao/article/details/88170522 1.下载最新版fiddler ,强烈建议在官网下载:https://w ...

  9. Easyui设置easyui-textbox不可编辑

    转载自:https://blog.csdn.net/qq_23113521/article/details/78801689 在easyui里由于easyui-textbox被封装,通过一般的jque ...

  10. Python3.9安装PySpider步骤及问题解决

    先写一些前言吧,自己感觉python已经有一定的基础了,但是在安装这个过程居然用了一下午,感觉有些收货,特地写下来与大家分享一下. PySpider是一个强大的网络爬虫系统,GitHub地址:http ...