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();
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值