Spring入门详细教程(二)
前言
本篇紧接着spring入门详细教程(一),建议阅读本篇前,先阅读第一篇。链接如下:
Spring入门详细教程(一) https://www.cnblogs.com/jichi/p/10165538.html
一、spring注入方式
1、set方法注入
<bean name="user" class="com.jichi.entity.User" >
<property name="name" value="小明"></property>
<property name="age" value="18"></property>
</bean>
2、构造方法注入
<bean name="user" class="com.jichi.entity.User" >
<constructor-arg name="name" value="小红" ></constructor-arg>
<constructor-arg name="age" value="50"></constructor-arg>
</bean>
3、p名称空间注入
xmlns:p="http://www.springframework.org/schema/p"
<bean name="user" class="com.jichi.entity.User" p:name="小白" p:age="10"></bean>
4、spel表达式注入
<bean name="user" class="com.jichi.entity.User">
<property name="name" value="小红"></property>
<property name="age" value="18"></property>
</bean>
<bean name="user1" class="com.jichi.entity.User">
<property name="name" value="#{user.name}"></property>
<property name="age" value="#{user.age}"></property>
</bean>
二、spring复杂类型注入
public class Collection { public String[] arr; public List<String> list; public Map<String,Object> map; public Properties props; public String[] getArr() {
return arr;
} public void setArr(String[] arr) {
this.arr = arr;
} public List<String> getList() {
return list;
} public void setList(List<String> list) {
this.list = list;
} public Map<String, Object> getMap() {
return map;
} public void setMap(Map<String, Object> map) {
this.map = map;
} public Properties getProps() {
return props;
} public void setProps(Properties props) {
this.props = props;
} @Override
public String toString() {
return "Collection [arr=" + Arrays.toString(arr) + ", list=" + list + ", map=" + map + ", props=" + props + "]";
} }
1、数组类型注入
<bean name="collect" class="com.jichi.entity.Collection">
<property name="arr">
<array>
<value>xiaohei</value>
<value>xiaobai</value>
</array>
</property>
</bean>
2、list类型注入
<bean name="collect" class="com.jichi.entity.Collection">
<property name="list">
<list>
<value>xiaohei</value>
<value>xiaobai</value>
</list>
</property>
</bean>
3、map类型注入
<bean name="collect" class="com.jichi.entity.Collection">
<property name="map">
<map>
<entry key="name" value="xiaohei"></entry>
<entry key="age" value="18"></entry>
</map>
</property>
</bean>
4、properties类型注入
<bean name="collect" class="com.jichi.entity.Collection">
<property name="props">
<props>
<prop key="name">xiaohei</prop>
<prop key="age">18</prop>
</props>
</property>
</bean>
三、配置spring随web项目启动初始化
在web.xml中配置。
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
四、spring的分配置文件
方式一:
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext1.xml","applicationContext2.xml")
方式二:
<import resource="applicationContext.xml"></import>
五、spring注解配置
1、开启注解扫描
<context:component-scan base-package="com.jichi.entity"></context:component-scan>
扫描com.jichi.entity下的所有类中的注解。
2、在类上添加注解
@Component
public class User {
}
六、spring常用注解
1、@Componet,@Controller,@Service,@Repository四个组件注解,作用在类上。四个注解并无区别,只是为了方便区分。
2、@Scope注解,作用在类上。
@Scope(scopeName="singleton") //单例模式
public class User {
}
@Scope(scopeName="prototype") //多例模式
public class User {
}
3、@Value用于注入普通类型值
第一种方式:作用在属性上,通过反射的filed值,破坏了对象的封装性。
@Value("xiaohei")
private String name;
第二种方式:通过set方法赋值,不破坏对象的封装性。
@Value("xiaobai")
public void setName(String name) {
this.name = name;
}
4、@Autowired,@Resource,@Qualifier注解
引用类型的装配方式,详细区别请看之前的博客。
@Autowired
private Car car;
@Resource
private Car car;
5、@PostConstruct与@PreDestroy
@PostConstruct //创建对象前调用
public void init(){
System.out.println("初始");
}
@PreDestroy //对象销毁前调用
public void destory(){
System.out.println("销毁");
}
七、spring与junit整合测试
1、导入spring基础包,与aop包和test包,可从lib中找到。
2、在测试类上添加注解
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class TestJunit { @Resource
private User user; @Test
public void test1(){
System.out.println(user);
}
}
Spring入门详细教程(二)的更多相关文章
- spring入门详细教程(五)
前言 本篇紧接着spring入门详细教程(三),建议阅读本篇前,先阅读第一篇,第二篇以及第三篇.链接如下: Spring入门详细教程(一) https://www.cnblogs.com/jichi/ ...
- Spring入门详细教程(四)
前言 本篇紧接着spring入门详细教程(三),建议阅读本篇前,先阅读第一篇,第二篇以及第三篇.链接如下: Spring入门详细教程(一) https://www.cnblogs.com/jichi/ ...
- Spring入门详细教程(三)
前言 本篇紧接着spring入门详细教程(二),建议阅读本篇前,先阅读第一篇和第二篇.链接如下: Spring入门详细教程(一) https://www.cnblogs.com/jichi/p/101 ...
- Spring入门详细教程(一)
一.spring概述 Spring是一个开放源代码的设计层面框架,他解决的是业务逻辑层和其他各层的松耦合问题,因此它将面向接口的编程思想贯穿整个系统应用.Spring是于2003 年兴起的一个轻量级的 ...
- ThinkJS框架入门详细教程(二)新手入门项目
一.准备工作 参考前一篇:ThinkJS框架入门详细教程(一)开发环境 安装thinkJS命令 npm install -g think-cli 监测是否安装成功 thinkjs -v 二.创建项目 ...
- 经典Spring入门基础教程详解
经典Spring入门基础教程详解 https://pan.baidu.com/s/1c016cI#list/path=%2Fsharelink2319398594-201713320584085%2F ...
- MVC5 + EF6 入门完整教程二
从前端的UI开始 MVC分离的比较好,开发顺序没有特别要求,先开发哪一部分都可以,这次我们主要讲解前端UI的部分. ASP.NET MVC抛弃了WebForm的一些特有的习惯,例如服务器端控件,Vie ...
- Spring Boot2 系列教程(二十)Spring Boot 整合JdbcTemplate 多数据源
多数据源配置也算是一个常见的开发需求,Spring 和 SpringBoot 中,对此都有相应的解决方案,不过一般来说,如果有多数据源的需求,我还是建议首选分布式数据库中间件 MyCat 去解决相关问 ...
- SpringBoot入门详细教程
一.SpringBoot入门 1.SpringBoot简介 SpringBoot是整个Spring技术栈的整合,来简化Spring应用开发,约定大于配置,去繁从简,just run 就能创建一 个独立 ...
随机推荐
- leetcode — rotate-image
import java.util.Arrays; /** * Source : https://oj.leetcode.com/problems/rotate-image/ * * Created b ...
- Go基础系列:WaitGroup用法说明
正常情况下,新激活的goroutine的结束过程是不可控制的,唯一可以保证终止goroutine的行为是main goroutine的终止.也就是说,我们并不知道哪个goroutine什么时候结束. ...
- “多个单核CPU”与“单个多核CPU”哪种方式性能较强?
多个单核CPU: 成本更高,因为每个CPU都需要一定的线路电路支持,这样对主板上布局布线极为不便.并且当运行多线程任务时,多线程间通信协同合作也是一个问题.依赖总线的传输,速度较慢,且每一个线程因为运 ...
- React Fiber源码分析 第一篇
先附上流程图一张 先由babel编译, 调用reactDOM.render,入参为element, container, callback, 打印出来可以看到element,container,cal ...
- 第一册:lesson3-4.
原文: A:My coat and my umbrella please?Here is my ticket. B:Thank you sir.Number five.Here is your umb ...
- 使用HttpWebRequest请求API接口以及其他网站资源
很多时候,我们项目需要其他网站的资源,而这个被请求的网站可能属于你们自己开发管理的网站.也可能是公网上其他网站对外开发的API接口,比如说腾讯的微信公众平台的API接口.各大短信服务商的短信API接口 ...
- Python正则进阶
目录 1.Python正则表达式模块 1.1 正则表达式处理字符串主要有四大功能 1.2 Python中re模块使用正则表达式的两种方法 1.3 正则表达式对象的常用方法 1.4 匹配对象的属性与方法 ...
- 为什么redis是单线程的?速度还这么快
为什么说Redis是单线程的? 为什么redis是单线程的?速度还这么快
- 4.3 explain 之 type
一.explain 的type类型 二.类型的排序 从最好到最差依次是: system > const > eq_ref > ref > range > index &g ...
- 【Java每日一题】20170309
20170308问题解析请点击今日问题下方的“[Java每日一题]20170309”查看(问题解析在公众号首发,公众号ID:weknow619) package Mar2017; public cla ...