注解开发1
配置文件
<?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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.neuedu.demo1"></context:component-scan>
</beans>
service注解
package com.neuedu.demo1;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
//@Component(value="userService")
//<bean id="userService" class="com.neuedu.demo1.UserService"
// init-method="setup" destroy-method="teardown" scope="">
// <property name="info" value="neusoft"></property>
// <property name="userDao" ref="userDao"></property>
//</bean>
@Service("userService")
@Scope(value="singleton")
public class UserService {
@Value("neusoft")
private String info;
@Autowired(自动匹配)
@Qualifier(value="userDao")(指向哪个引用对象)
// @Resource(name="userDao")
private UserDao userDao;
public void sayHello(){
System.out.println("Hello spring ioc annotation"+"--"+info+"--"+userDao);
}
@PostConstruct
public void setup(){
System.out.println("初始化方法。。");
}
@PreDestroy
public void teardown(){
System.out.println("销毁方法。。");
}
}
service注解开发时的引用对象代码
package com.neuedu.demo1;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
//@Component
@Repository("userDao")
//<bean id="userDao" class="com.neuedu.demo1.UserDao">
public class UserDao {
}
测试类
package com.neuedu.demo1;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringTest1 {
@Test
public void demo1(){
ClassPathXmlApplicationContext applicationContext =
new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService =
(UserService) applicationContext.getBean("userService");
// userService.sayHello();
UserService userService1 =
(UserService) applicationContext.getBean("userService");
System.out.println(userService);
System.out.println(userService1);
applicationContext.close();
}
}
注解开发2
配置文件
<?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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="userService" class="com.neuedu.demo2.UserService"></bean>
</beans>
service类
package com.neuedu.demo2;
public class UserService {
public void sayHello(){
System.out.println("Hello Junit...");
}
}
测试类
package com.neuedu.demo2;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext2.xml")
public class SpringTest2 {
@Autowired
@Qualifier("userService")
// @Resource(name="userService")
private UserService userService;
@Test
public void demo1(){
userService.sayHello();
}
}
网友评论