概述
最近在看spring framework 的官方文档,看到spring的全注解的方式来dao,serviece,controller,没有xml,感觉非常的方便,所以在此记录一下
环境:maven项目,jdk1.8,spring-4.3.5
1.首先配置spring配置java类,此操作相当于原来的配置xml文件,java类如下:
SpringIocXmlConfig.java类
package org.spring.ioc.core.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration//表明这是一个配置管理java类,这个java类相当于spring.xml 文件
@ComponentScan(basePackages="org.spring.ioc.core")//指明采用注解的方式--扫描org.spring.ioc.core这个包下(包括子包)所用的注解,例如@Service,@Controller,@Autowire等等
public class SpringIocXmlConfig {
}
2.配置dao层,有两个文件SpringIocDao.java接口和SpringIocDaoImpl.java实现类
SpringIocDao.java类
package org.spring.ioc.core.dao;
public interface SpringIocDao {
void testAnnotation();
}
SpringIocDaoImpl.java实现类
package org.spring.ioc.core.dao.impl;
import org.spring.ioc.core.dao.SpringIocDao;
import org.springframework.stereotype.Repository;
@Repository//默认情况下向IOC 容器中注入一个名为“springIocDaoImpl",即在默认情况下注入的是类名(首字母小写)这里可以用@Repository("springIocDaoImpl")代替
public class SpringIocDaoImpl implements SpringIocDao {
@Override
public void testAnnotation() {
System.out.println("spring ioc annotation test successful");
}
}
3.配置service层,两个文件SpringIocService接口,SpringIocServiceImpl实现类
SpringIocService接口
package org.spring.ioc.core.service;
public interface SpringIocService {
void testAnnotation();
}
SpringIocServiceImpl实现类
package org.spring.ioc.core.service.impl;
import org.spring.ioc.core.dao.SpringIocDao;
import org.spring.ioc.core.service.SpringIocService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service//解释同@Repository
public class SpringIocServiceImpl implements SpringIocService {
@Autowired//默认是按类型进行装配,如果有两个SpringIocDao的实现,则需用@Qualifier("实现类在IOC中的名称")进行注释
public SpringIocDao springIocDao;
@Override
public void testAnnotation() {
springIocDao.testAnnotation();
}
}
4.controller层 ,一个java文件,SpringIocController.java文件
SpringIocController.java类
package org.spring.ioc.core.controller;
import org.spring.ioc.core.bean.NoInterfaceBean;
import org.spring.ioc.core.service.SpringIocService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
@Controller//经过测试发现@Controller默认情况下是向spring IOC容器中注入一个名为“springIocController”的bean,相当于@Controller("springIocController")
public class SpringIocController {
@Autowired
private SpringIocService springIocService;
@Autowired
private NoInterfaceBean noInterfaceBean;
public void testAnnotation(){
springIocService.testAnnotation();
}
public void testNointerfaceAnnotation(){
noInterfaceBean.testNointerfaceAnnotation();
}
}
5.测试类
SpringAnnotationMain.java类
package org.spring.ioc.core.test;
import org.spring.ioc.core.config.SpringIocXmlConfig;
import org.spring.ioc.core.controller.SpringIocController;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* 测试spring(采用全注解的方式)
* @author lijun
*
*/
public class SpringAnnotationMain {
public static void main(String[] args) {
@SuppressWarnings("resource")
ApplicationContext context = new AnnotationConfigApplicationContext(SpringIocXmlConfig.class);//加载配置文件,这里是加载一个java类,相当于以前的加载xml
SpringIocController controller = context.getBean("springIocController",SpringIocController.class);//得到SpringIocController实例
controller.testAnnotation();
controller.testNointerfaceAnnotation();
}
}
根据以上代码即可搭建一个全注解的springIOC三层结构,输出的结果为
log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
spring ioc annotation test successful
spring ioc annotation no interface
最后
以上就是彪壮香水为你收集整理的spring全注解的方式构建web三层结构的全部内容,希望文章能够帮你解决spring全注解的方式构建web三层结构所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复