入门

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.6</version>
</dependency>
  • 实体类
package com.pojo;

public class Hello {
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;
}
}
  • 配置beans.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="hello" class="com.pojo.Hello">
<property name="name" value="张三"></property>
<property name="age" value="18"></property>
</bean>
</beans>
  • 测试
@Test
public void test1() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Hello hello = (Hello) context.getBean("hello");
System.out.println(hello);
}

Set方式注入

  • Student,Address
public class Address {
private String address; public String getAddress() {
return address;
} public void setAddress(String address) {
this.address = address;
}
}
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobbies;
private Map<String,String> card;
private Set<String> games;
private String wife;
private Properties info; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Address getAddress() {
return address;
} public void setAddress(Address address) {
this.address = address;
} public String[] getBooks() {
return books;
} public void setBooks(String[] books) {
this.books = books;
} public List<String> getHobbies() {
return hobbies;
} public void setHobbies(List<String> hobbies) {
this.hobbies = hobbies;
} public Map<String, String> getCard() {
return card;
} public void setCard(Map<String, String> card) {
this.card = card;
} public Set<String> getGames() {
return games;
} public void setGames(Set<String> games) {
this.games = games;
} public String getWife() {
return wife;
} public void setWife(String wife) {
this.wife = wife;
} public Properties getInfo() {
return info;
} public void setInfo(Properties info) {
this.info = info;
} @Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", address=" + address +
", books=" + Arrays.toString(books) +
", hobbies=" + hobbies +
", card=" + card +
", games=" + games +
", wife='" + wife + '\'' +
", info=" + info +
'}';
}
}
  • beans.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="address" class="com.pojo.Address"/>
<bean id="student" class="com.pojo.Student"> <property name="name" value="张三"/> <property name="address" ref="address"/> <property name="books">
<array>
<value>红楼梦</value>
<value>水浒传</value>
</array>
</property> <property name="hobbies">
<list>
<value>唱歌</value>
<value>编程</value>
</list>
</property> <property name="card">
<map>
<entry key="身份证" value="123"/>
<entry key="大学" value="哔哩哔哩"/>
</map>
</property> <property name="games">
<set>
<value>lol</value>
<value>王者</value>
</set>
</property> <property name="wife">
<null>/</null>
</property> <property name="info">
<props>
<prop key="学号">123</prop>
<prop key="性别">男</prop>
</props>
</property>
</bean> </beans>

拓展方式注入

  • User.java
package com.pojo;

public class User {
private String name;
private int age; public User(String name, int age) {
this.name = name;
this.age = age;
} public User() {
} 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 "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
  • userbeans.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:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="user" class="com.pojo.User" p:name="张三" p:age="18"></bean> <bean id="users" class="com.pojo.User" c:name="里斯" c:age="19"></bean>
</beans>
  • 注意点;p命名和c命名空间不能直接使用,需要导入xml约束!
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"

bean的作用域scope

<bean id="users" class="com.pojo.User" c:name="里斯" c:age="19" scope="prototype"></bean>
  • 单例模式singleton
  • 原型模式prototype
  • 其余的request、session、application、这些个只能在web开发中使用到!

Spring配置及依赖注入的更多相关文章

  1. Spring Framework------>version4.3.5.RELAESE----->Reference Documentation学习心得----->Spring Framework的依赖注入和控制反转

    Dependency Injection and Inversion of Control 1.概述: 1.1相关概念 bean:由IoC容器所管理的对象,也即各个类实例化所得对象都叫做bean 控制 ...

  2. 谈谈自己了解的spring.NET的依赖注入

         spring.net里实现了控制反转IOC(Inversion of control),也即依赖注入DI(Dependency Injection),以达到解耦的目的,实现模块的组件化.程序 ...

  3. Spring学习(三)——Spring中的依赖注入的方式

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

  4. spring六种种依赖注入方式

    平常的java开发中,程序员在某个类中需要依赖其它类的方法,则通常是new一个依赖类再调用类实例的方法,这种开发存在的问题是new的类实例不好统一管理,spring提出了依赖注入的思想,即依赖类不由程 ...

  5. spring.NET的依赖注入

    谈谈自己了解的spring.NET的依赖注入   spring.net里实现了控制反转IOC(Inversion of control),也即依赖注入DI(Dependency Injection), ...

  6. spring 四种依赖注入方式以及注解注入方式

    平常的java开发中,程序员在某个类中需要依赖其它类的方法,则通常是new一个依赖类再调用类实例的方法,这种开发存在的问题是new的类实例不好统一管理,spring提出了依赖注入的思想,即依赖类不由程 ...

  7. JavaEE开发之Spring中的依赖注入与AOP

    上篇博客我们系统的聊了<JavaEE开发之基于Eclipse的环境搭建以及Maven Web App的创建>,并在之前的博客中我们聊了依赖注入的相关东西,并且使用Objective-C的R ...

  8. JavaEE开发之Spring中的依赖注入与AOP编程

    上篇博客我们系统的聊了<JavaEE开发之基于Eclipse的环境搭建以及Maven Web App的创建>,并在之前的博客中我们聊了依赖注入的相关东西,并且使用Objective-C的R ...

  9. spring几种依赖注入方式以及ref-local/bean,factory-bean,factory-method区别联系

    平常的java开发中,程序员在某个类中需要依赖其它类的方法,则通常是new一个依赖类再调用类实例的方法,这种开发存在的问题是new的类实例不好统一管理,spring提出了依赖注入的思想,即依赖类不由程 ...

随机推荐

  1. Grep 命令有什么用? 如何忽略大小写? 如何查找不含 该串的行?

    是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印 出来. grep [stringSTRING] filename grep [^string] filename

  2. Leetcode1——两数之和 详细解析

    Leetcode1--两数之和 题目分类:数组.哈希表的应用 1. 题目描述 给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数 ...

  3. 无人车系统仿真相关软件介绍-dSPACE

    今天本来是想简单的介绍一下dSPACE的Automotive simulation models(简称ASM),但是想想还是把dSPACE这个公司的整个开发流程写一下.这样也可以了解一下汽车的整个软件 ...

  4. js 简易模块加载器 示例分析

    前端模块化 关注前端技术发展的各位亲们,肯定对模块化开发这个名词不陌生.随着前端工程越来越复杂,代码越来越多,模块化成了必不可免的趋势. 各种标准 由于javascript本身并没有制定相关标准(当然 ...

  5. 【uniapp 开发】如何给边框添加阴影效果

    css的box-shadow是用来添加边框阴影效果的. 属性值详解: 1.inset 可选值,默认阴影在盒子外 使用inset后,阴影在盒子内,即使指定边框或者透明边框,阴影依然存在. 2. 这是头两 ...

  6. PAT B1086 就不告诉你

    题目描述: 做作业的时候,邻座的小盆友问你:"五乘以七等于多少?"你应该不失礼貌地围笑着告诉他:"五十三."本题就要求你,对任何一对给定的正整数,倒着输出它们的 ...

  7. PAT B1013 数素数

    输入样例: 5 27   输出样例: 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 解题思路: 从2开始 ...

  8. 使用React实现一个TodoList案例

    1.效果图: 2.项目源码 3.源码 TodoList.js import React, { Component, Fragment } from 'react'; import TodoItem f ...

  9. MFC软件国际化的几个问题及其解决方案

    作者:马健 邮箱:stronghorse_mj@hotmail.com主页:https://www.cnblogs.com/stronghorse/ 以前我以为PDG相关软件只会在国内流行,所以发行简 ...

  10. Java中最早期的集合Vector

    1.Vector类可以实现可增长的对象数组.与数组一样,它包含可以使用整数索引进行访问的组件.但是,Vector 的大小可以根据需要增大或缩小,以适应创建 Vector 后进行添加或移除项的操作. 2 ...