SpringMVC的Java配置替换spring-mvc.xml文件
来源:互联网 发布:java荣誉勋章神兵天降 编辑:程序博客网 时间:2024/05/18 01:41
Java类替换spring-mvc.xml。SpringConfig.java相当于spring-mvc.xml文件,@Bean注入bean作用在方法上,也可以在所要注入的类上直接加注释(@Service,@Repository,@Controller,@AutoWired等)
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]
阅读全文
0 0
- SpringMVC的Java配置替换spring-mvc.xml文件
- spring-mvc.xml文件的配置方式
- spring mvc springmvc.xml配置说明
- spring mvc xml文件配置
- spring-mvc.xml文件的配置(官方文件)
- Spring MVC的web.xml文件配置详解
- Springmvc spring xml文件配置问题
- 零xml配置Spring MVC文件上传
- spring MVC 学习笔记与总结1----spring MVC 介绍与基本的.xml文件配置
- spring mvc 配置web.xml servlet.xml文件配置以及出现异常的解决方案
- 【spring Mvc】SpringMVC 文件上传配置,多文件上传,使用的MultipartFile
- 三,基于Java配置、无web.xml文件方式配置Spring MVC
- spring-mvc.xml配置
- spring-mvc.xml配置
- spring mvc xml配置
- springmvc+spring的web.xml配置
- Spring MVC学习-----------springMVC-mvc.xml
- spring mvc的web.xml的配置
- echarts图例分页显示
- 财务报表模块概述
- VMware Workstation虚拟机平台安装Linux-Ubuntu16.04图文教程
- pl- 配置远程打印机,用linux命令行打印
- 从相册读取大文件dataWithContentsOfURL?
- SpringMVC的Java配置替换spring-mvc.xml文件
- org.bouncycastle.jce.provider.JCERSAPrivateCrtKey
- scala-单机wordcount
- 不安装oracle客户端,plsql developer的安装教程
- MalformedByteSequenceException: 3 字节的 UTF-8 序列的字节 3 无效。
- 数组A中任意两个相邻元素大小相差1,在其中查找某个数。
- 移动端多行文本在横向屏和竖向屏下如何保持字号大小一致
- 2、HTML 学习记录——基础
- UniStorm基础设置