一、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. Spring Boot 2.x基础教程:使用Elastic Job实现定时任务

    上一篇,我们介绍了如何使用Spring Boot自带的@Scheduled注解实现定时任务.文末也提及了这种方式的局限性.当在集群环境下的时候,如果任务的执行或操作依赖一些共享资源的话,就会存在竞争关 ...

  2. Maven作用及应用

    1.简介 Maven是一个项目管理的Java 工具,在JavaEE中,我们可以使用Maven方便地管理团队合作的项目,现在我们在学习JavaEE框架,使用Maven可以管理类库,有效方便地供团队中的其 ...

  3. python中的abstractmethod

    # -*- coding: utf-8 -*- from abc import ABC ,abstractclassmethod from collections import namedtuple ...

  4. 记一次jenkins svn构建过程

    本文主要参考:maven实战第11章Hudson持续集成 安装Hudson,Hudson插件下载不了,尝试结果未果,转而使用Jenkins 放入tomcat的webapp目录,在bin下点击start ...

  5. 云平台制作(1)-OPC Client取数模块的制作

    近来由于工程需要,基于OPC DA 2.0搭建通用的取数模块,与远程webscoket服务端连接,并传输数据.在网上找了些资料,修改相应网友公开的源代码,基本达到要求,特供大家参考. 1.实体类 us ...

  6. js中变量提升和函数提升

    变量提升和函数提升的总结 我们在学习JavaScript时,会遇到变量提升和函数提升的问题,为了理清这个问题,现做总结如下,希望对初学者能有所帮助 我们都知道 var 声明的变量有变量提升,而 let ...

  7. a = input(a, yymmdd10.)引发的问题

    在数据清理过程中,经常会遇到以文本储存的日期型数据,这种数据不能直接进行分析,需要先将其转化为以数值存储的格式. 首先准备数据集: data data1; input a :$10. b :$10. ...

  8. DC-6靶机

    仅供个人娱乐 靶机信息 下载地址:https://download.vulnhub.com/dc/DC-6.zip 一.主机发现 nmap -sn 192.168.216.0/24 二.端口扫描 nm ...

  9. netty系列之:netty架构概述

    目录 简介 netty架构图 丰富的Buffer数据机构 零拷贝 统一的API 事件驱动 其他优秀的特性 总结 简介 Netty为什么这么优秀,它在JDK本身的NIO基础上又做了什么改进呢?它的架构和 ...

  10. 攻防世界Web区部分题解

    攻防世界Web区部分题解   前言:PHP序列化就是把代码中所有的 对象 , 类 , 数组 , 变量 , 匿名函数等全部转换为一个字符串 , 提供给用户传输和存储 . 而反序列化就是把字符串重新转换为 ...