注解、context和util的使用

注解、contextutil的使用

@Autowired

@PostConstruct

@PreDestroy

@Test

以上这都是注解

cn.csdn.service. Address

package cn.csdn.service;

import java.util.List;

public class Address {

    private List<String> list;

    public List<String> getList() {

       return list;

    }

    public void setList(List<String> list) {

       this.list = list;

    }

}

cn.csdn.service. Emp

package cn.csdn.service;

import javax.annotation.PostConstruct;

import javax.annotation.PreDestroy;

import org.springframework.beans.factory.annotation.Autowired;

public class Emp {

    private Address address;

    public Address getAddress() {

       return address;

    }

    @Autowired

    public void setAddress(Address address) {

       this.address = address;

    }

    @PostConstruct

    public void init(){

       System.out.println("初始化bean的操作...");

    }

    public Emp() {

       System.out.println("初始化...");

    }

    @PreDestroy

    public void distory(){

       System.out.println("销毁的操作...");

    }

}

配置文件

<?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:context="http://www.springframework.org/schema/context"

        xmlns:util="http://www.springframework.org/schema/util"

    xsi:schemaLocation="http://www.springframework.org/schema/beans

    http://www.springframework.org/schema/beans/spring-beans-2.0.xsd

    http://www.springframework.org/schema/context

    http://www.springframework.org/schema/context/spring-context-2.5.xsd

    http://www.springframework.org/schema/util

    http://www.springframework.org/schema/util/spring-util-2.5.xsd">

 

<bean id="address" class="cn.csdn.service.Address">

<property name="list">

    <util:list>

       <value>kouxiaolin</value>

    </util:list>

</property>

</bean>

<bean id="emp" class="cn.csdn.service.Emp"></bean>

<context:annotation-config/>

<!--

    //生命周期的bean

    <bean id="commonAnnotationBeanPostProcessor"

    class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/>

    //自动装配

bean <bean id="autowiredAnnotationBeanPostProcessor"

    class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor">

    </bean>

    -->

</beans>

Contextutil

xmlns:context="http://www.springframework.org/schema/context"

xmlns:util="http://www.springframework.org/schema/util"

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-2.5.xsd

http://www.springframework.org/schema/util

http://www.springframework.org/schema/util/spring-util-2.5.xsd">

它们既可以手动配置也可以自动生成

自动生成:在配置文件视图编辑器,右击beans,再点击Editor namespace,出现对话框,点击Add,选择Specify new Namespace,点击Browse选择select xml Catalog entry,选择即可。

Prefix是前缀

Namespace name:命名空间

cn.csdn.service. App

package cn.csdn.service;

import org.junit.Test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.AbstractApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {

    @Test

    public void test(){

       ApplicationContext ac = new ClassPathXmlApplicationContext("app*.xml");

       Emp emp = (Emp) ac.getBean("emp");

       System.out.println(emp);

        ((AbstractApplicationContext)ac).close();

    }

}

你可能感兴趣的:(spring,xml,bean,JUnit)