SpringMVC的Java配置替换spring-mvc.xml文件
2017-09-29 17:26
651 查看
Java类替换spring-mvc.xml。SpringConfig.java相当于spring-mvc.xml文件,@Bean注入bean作用在方法上,也可以在所要注入的类上直接加注释(@Service,@Repository,@Controller,@AutoWired等)
Person实体类
StudentDao类
StudentServices类
测试类
测试结果
package com.csl.demo.annotation1; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @ComponentScan(basePackages= {"com.csl.demo.annotation"})//相当与<context:component-scan base-package=""/> @Configuration//替换spring-mvc.xml文件 public class SpringConfig { //注入bean @Bean(name="personServices") public PersonServices getPersonServices() { PersonServices ps = new PersonServices(); //bean中的属性参照 ps.setPersonDao(getPersonDao()); return ps; } //注入bean @Bean(name="personDao") public PersonDao getPersonDao() { return new PersonDao(); } }
Person实体类
package com.csl.demo.annotation1; import java.util.Date; public class Person { private int id; private String name; private Date birthday; //.......省略代码 }
StudentDao类
package com.csl.demo.annotation1; import java.util.ArrayList; import java.util.Date; import java.util.List; public class PersonDao { @SuppressWarnings("deprecation") List<Person> listPerson(){ List<Person> list = new ArrayList<Person>(); for (int i = 0; i < 6; i++) { Person p = new Person(i, "Person"+i, new Date(117,i,10+i)); list.add(p); } return list; } }
StudentServices类
package com.csl.demo.annotation1; import java.util.List; public class PersonServices { private PersonDao personDao; public void setPersonDao(PersonDao personDao) { this.personDao = personDao; } public List<Person> listPerson() { return personDao.listPerson(); } }
测试类
package com.csl.demo.annotation1; import java.util.List; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import com.csl.demo.datasources.SpringConfig2; import com.csl.demo.datasources.StudentServices; public class Tests { public static void main(String[] args) { //創建spring容器 @SuppressWarnings("resource") AnnotationConfigApplicationContext aac = new AnnotationConfigApplicationContext(SpringConfig.class); PersonServices ps = (PersonServices) aac.getBean("personServices"); List<Person> list = ps.listPerson(); for (Person p : list) { System.out.println(p); } aac.destroy();//銷毀 } }
测试结果
Person [id=0, name=Person0, birthday=Tue Jan 10 00:00:00 CST 2017] Person [id=1, name=Person1, birthday=Sat Feb 11 00:00:00 CST 2017] Person [id=2, name=Person2, birthday=Sun Mar 12 00:00:00 CST 2017] Person [id=3, name=Person3, birthday=Thu Apr 13 00:00:00 CST 2017] Person [id=4, name=Person4, birthday=Sun May 14 00:00:00 CST 2017] Person [id=5, name=Person5, birthday=Thu Jun 15 00:00:00 CST 2017]
相关文章推荐
- springMVC配置文件spring-servlet.xml中<mvc:annotation-driven />的意义
- SpringMVC(1):基础配置文件web.xml 和 <servlet-name>-servlet.xml / springmvc.xml
- springMvc的配置文件springmvc.xml
- 【SpringMVC】加载自定义目录下的springmvc.xml配置文件(三)
- SpringMVC加载自定义目录下的springmvc.xml配置文件
- SPRING IN ACTION 第4版笔记-第七章Advanced Spring MVC-002- 在xml中引用Java配置文件,声明DispatcherServlet、ContextLoaderListener
- SpringMVC框架的视图解析器ViewResolver想用html不用jsp的spring-mvc.xml文件配置
- 第2.1.5章 WEB系统最佳实践Spring文件配置之spring-mvc.xml
- spring-mvc.xml文件的配置(官方文件)
- java文件读取web-inf下的spring的xml配置文件
- Spring MVC 教程,快速入门,深入分析――springMVC-mvc.xml 配置文件片段讲解
- SpringMVC+Spring4+hibernate5使用java类配置spring和springmvc环境(不用配置web.xml)
- 【Spring实战】----springMVC配置文件*-servlet.xml的解析
- SpringMVC + mybatis 无法读取JAR包中的XML配置文件问题
- java spring配置文件applicationcontext.xml的使用
- SSM:spring+springmvc+mybatis框架中的XML配置文件功能详细解释
- SpringMvc教程(六)--springMVC-mvc.xml 配置文件片段讲解
- springMVC-mvc.xml 配置文件片段讲解 (未使用默认配置文件名)
- java中spring+springMVC+Mybatis主流框架maven的pom.xml配置参考
- SSM:spring+springmvc+mybatis框架中的XML配置文件功能详细解释