前言

本篇紧接着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入门详细教程(二)的更多相关文章

  1. spring入门详细教程(五)

    前言 本篇紧接着spring入门详细教程(三),建议阅读本篇前,先阅读第一篇,第二篇以及第三篇.链接如下: Spring入门详细教程(一) https://www.cnblogs.com/jichi/ ...

  2. Spring入门详细教程(四)

    前言 本篇紧接着spring入门详细教程(三),建议阅读本篇前,先阅读第一篇,第二篇以及第三篇.链接如下: Spring入门详细教程(一) https://www.cnblogs.com/jichi/ ...

  3. Spring入门详细教程(三)

    前言 本篇紧接着spring入门详细教程(二),建议阅读本篇前,先阅读第一篇和第二篇.链接如下: Spring入门详细教程(一) https://www.cnblogs.com/jichi/p/101 ...

  4. Spring入门详细教程(一)

    一.spring概述 Spring是一个开放源代码的设计层面框架,他解决的是业务逻辑层和其他各层的松耦合问题,因此它将面向接口的编程思想贯穿整个系统应用.Spring是于2003 年兴起的一个轻量级的 ...

  5. ThinkJS框架入门详细教程(二)新手入门项目

    一.准备工作 参考前一篇:ThinkJS框架入门详细教程(一)开发环境 安装thinkJS命令 npm install -g think-cli 监测是否安装成功 thinkjs -v 二.创建项目 ...

  6. 经典Spring入门基础教程详解

    经典Spring入门基础教程详解 https://pan.baidu.com/s/1c016cI#list/path=%2Fsharelink2319398594-201713320584085%2F ...

  7. MVC5 + EF6 入门完整教程二

    从前端的UI开始 MVC分离的比较好,开发顺序没有特别要求,先开发哪一部分都可以,这次我们主要讲解前端UI的部分. ASP.NET MVC抛弃了WebForm的一些特有的习惯,例如服务器端控件,Vie ...

  8. Spring Boot2 系列教程(二十)Spring Boot 整合JdbcTemplate 多数据源

    多数据源配置也算是一个常见的开发需求,Spring 和 SpringBoot 中,对此都有相应的解决方案,不过一般来说,如果有多数据源的需求,我还是建议首选分布式数据库中间件 MyCat 去解决相关问 ...

  9. SpringBoot入门详细教程

    一.SpringBoot入门 1.SpringBoot简介 SpringBoot是整个Spring技术栈的整合,来简化Spring应用开发,约定大于配置,去繁从简,just run 就能创建一 个独立 ...

随机推荐

  1. Android UI(一)Layout 背景局部Shape圆角设计

    Jeff Lee blog:   http://www.cnblogs.com/Alandre/  (泥沙砖瓦浆木匠),retain the url when reproduced ! Thanks ...

  2. Logback中如何自定义灵活的日志过滤规则

    当我们需要对日志的打印要做一些范围的控制的时候,通常都是通过为各个Appender设置不同的Filter配置来实现.在Logback中自带了两个过滤器实现:ch.qos.logback.classic ...

  3. [转]分别使用Node.js Express 和 Koa 做简单的登录页

    本文转自:https://blog.csdn.net/weixin_38498554/article/details/79204240 刚刚学了Koa2,由于学的不是很深,并没有感受到网上所说的Koa ...

  4. wpf学习20180606

    对象元素的子元素 有三类子元素:内容属性.集合项.值(类型转换) ------------------------------------------------------------------- ...

  5. ABP框架 sql语句(转载)

    ABP.Core实现SQL语句仓储,支持EF.Core兼容的数据库  来源:https://blog.csdn.net/qq_28699537/article/details/80522680?tds ...

  6. SpringBoot 配置静态资源映射

    SpringBoot 配置静态资源映射 (嵌入式servlet容器)先决知识 request.getSession().getServletContext().getRealPath("/& ...

  7. 用kafka实现消息推送

    一个人知道的Topic是单点推送,大家都知道Topic是广播. kafka消息消费机制: 1.广播消费:通过定义topic前缀来标识属于广播的消息(例如:topicname:gonggao153568 ...

  8. python爬虫入门urllib库的使用

    urllib库的使用,非常简单. import urllib2 response = urllib2.urlopen("http://www.baidu.com") print r ...

  9. react-conponent-todo

    <!DOCTYPE html> <html> <head> <script src="../../build/react.js">& ...

  10. VMWAR-workstatuon : 安装win10、server 2008 r2、server 2012 r2

    最新版的VMWAR 不是很文档,建议大家还是下载稳定版,截止当前最新版的为15,用了,有点问题. 换成14~ 可以了.(15创建虚拟机安装vmware tools 怎么都安装不了). 关于创建虚拟机, ...