web开发,提供3个@Component注解衍生注解(功能一样)取代
@Repository :dao层
@Service:service层
@Controller:web层
套路是这样的,service层调动dao层,web层调动service层,展示出来
下面就是案例演示了
dao层代码:
package com.fly.spring.annotation;
import org.springframework.stereotype.Repository;
@Repository("sudentDao")
public class SudentDao {
public void save(){
System.out.println("dao层");
}
}
service层代码:
package com.fly.spring.annotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
@Service
public class PersonService {
private SudentDao sudentDao;
@Autowired
@Qualifier("sudentDao") // 按照名称注入
public void setSudentDao(SudentDao sudentDao){
this.sudentDao = sudentDao;
}
// 给web层调用的方法
public void getString(){
sudentDao.save();
}
}
web层代码:
package com.fly.spring.annotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
@Controller("teacherWeb")
public class TeacherWeb {
@Autowired //默认按照类型
private PersonService person;
public void execute(){
person.getString();
}
}
配置文件代码applicationContext.xml:
<?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.fly.spring.annotation"></context:component-scan>
</beans>
测试代码:
package com.fly.spring.annotation;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestDemo {
@Test
public void demo(){
String xmlpath = "com/fly/spring/annotation/applicationContext.xml";
ApplicationContext context = new ClassPathXmlApplicationContext(xmlpath);
TeacherWeb bean = (TeacherWeb) context.getBean("teacherWeb");
bean.execute();
}
}
测试结果:
2017-3-2 10:14:04 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1b016632: startup date [Thu Mar 02 10:14:04 CST 2017]; root of context hierarchy
2017-3-2 10:14:04 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [com/fly/spring/annotation/applicationContext.xml]
2017-3-2 10:14:06 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@43ee148b: defining beans [personService,sudentDao,teacherWeb,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy
dao层