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);
    }
}

 

posted @ 2018-12-25 21:31  经典鸡翅  阅读(2965)  评论(0编辑  收藏  举报