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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM