Spring use annotation

今天本来是要做spring+jpa的例子的但是奈何 老是报错 所以就开始研究spring的annotation的例子 顺便自己做了做 留在博客 以免自己忘记 老同志们看了 请绕开这个初级的博文
首先是配置文件
<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd">
          <context:component-scan base-package="com.lee"/>
</beans>

这是要注入的类内容

package com.lee.test;


import org.springframework.stereotype.Repository;
/**
* @Service用于标注业务层组件、 @Controller用于标注控制层组件(如struts中的action)、
* @Repository用于标注数据访问组件,即DAO组件。而@Component泛指组件,
* 当组件不好归类的时候,我们可以使用这个注解进行标注。
* @author Administrator
*
*/
@Repository
public class Test {

public void say(){
System.out.println("注解的方式");
}

}


package com.lee.test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
@Service("xxx")//在扫描时可以自定义名字
@Scope("prototype")
public class Say {

private Test test;

@Autowired
public void setTest(Test test) {
this.test = test;
}

public void mySay(){
test.say();
}



}

再来个测试类

package com.lee.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Heh {
/**
* 正常的基于XML的setter模式
* <?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd">
          <bean id="test" class="com.lee.test.Test"/>
          <bean id="say" class="com.lee.test.Say">
          <property name="test" ref="test"></property>
          </bean>
          </beans>
*
*/
@Test
public void testSay(){

ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
Say say = (Say) ac.getBean("say");
say.mySay();
}
/**
* 采用注解的形式
* spring配置文件
* <?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd">
          <context:annotation-config/>
          <bean id="test" class="com.lee.test.Test"/>
          <bean id="say" class="com.lee.test.Say"/>
          </beans>
*/

@Test
public void testAnnotation(){
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
Say say = (Say) ac.getBean("say");
say.mySay();
}
/**
* 使用自动扫描的Spring的配置文件
* <?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd">
          <context:component-scan base-package="com.lee"/>
</beans>
*/

@Test
public void testAuto(){
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
Say say = (Say) ac.getBean("xxx");
Say say2 = (Say) ac.getBean("xxx");
System.out.println(say == say2);
say.mySay();
}
}

你可能感兴趣的:(spring,annotation,Beans.xml)