Spring利器之包扫描器
在学习Spring这门技术中为了大大减少applicationContext.xml配置的代码量于是有了包扫描器。
闲话不多说我们马上来实现一下吧
示例架构如下:

第一步我们先来修改我们的配置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="cn.lxp.entity"></context:component-scan>
</beans>
第二步我们先创建实体类StudentInfo
package cn.lxp.entity; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; @Component("info")
public class StudentInfo {
//赋值
@Value("Mr.DaPeng")
private String Name;
//赋值
@Value("18")
private String age; @Override
public String toString() {
return "StudentInfo [Name=" + Name + ", age=" + age + "]";
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
} }
第三步创建一个测试类SpringTest来测试我们的代码
package cn.lxp.test; import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.lxp.entity.StudentInfo; public class SpringTest { @Test
public void testOne(){
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); StudentInfo stu=(StudentInfo)context.getBean("info"); System.out.println(stu);
}
}
这样我们就不用再配置文件中写很多配置的东西了直接用包扫描器为我们定义的属性赋值了

Spring利器之包扫描器的更多相关文章
- Java 包扫描器
包扫描器 获取一个包下的所有类,然后使用默认的类加载器加载到内存中 public static List<Class<?>> scanByPackage(String pack ...
- 各个版本spring的jar包以及源码下载地址
各个版本spring的jar包以及源码下载地址,目前最高版本到spring4.1.2,留存备用: http://maven.springframework.org/release/org/spring ...
- Spring众多jar包的特点,及Spring jar包官网下载方法
下面给大家说说spring众多jar包的特点吧,无论对于初学spring的新手,还是spring高手,这篇文章都会给大家带来知识上的收获,如果你已经十分熟悉本文内容就当做一次温故知新吧.spring. ...
- 笔面试复习(spring常用.jar包/事务/控制反转/bean对象管理和创建/springMVC工作原理/sql查询)
###spring常用jar包1.spring.jar是包含有完整发布模块的单个jar包.2.org.springframework.aop包含在应用中使用Spring的AOP特性时所需要的类.3.o ...
- spring相关jar包的作用讲解(转)
Spring各个jar包作用 原文链接:https://www.cnblogs.com/1995hxt/p/5255210.html Spring AOP:Spring的面向切面编程,提供AOP ...
- spring的jar包以及相关的API文档的下载方式
转自:http://blog.csdn.net/yuexianchang/article/details/53583327 侵删 感谢原创 首先是进入官网:https://spring.io/ 如图所 ...
- Spring各个jar包详解
Spring各jar包详解 spring.jar 是包含有完整发布模块的单个jar 包.但是不包括mock.jar,aspects.jar, spring-portlet.jar, and sprin ...
- Spring框架针对dao层的jdbcTemplate操作crud之delete删除数据库操作 Spring相关Jar包下载
首先,找齐Spring框架中IoC功能.aop功能.JdbcTemplate功能所需的jar包,当前13个Jar包 1.Spring压缩包中的四个核心JAR包,实现IoC控制反转的根据xml配置文件或 ...
- spring的jar包的下载、说明
spring的jar包官方下载地址:完整链接:https://repo.spring.io/webapp/#/artifacts/browse/tree/General/libs-release-lo ...
随机推荐
- Flowplayer-playlist
SOURCE URL: https://flowplayer.org/docs/playlist.html HTML layout Here is a typical setup for a play ...
- GMF中,删除节点和连线的另一种实现
问题 在GMF中,如果需要programmatically删除节点或连线,在google中我们很容易搜索到<GMF中,删除节点和连线的实现>一文(我并不确定这是原创作者的原始链接),很多人 ...
- [问题2014S11] 解答
[问题2014S11] 解答 我们先引用一下复旦高代书 P310 的习题 6, 其证明可参考白皮书 P257 的例 8.33: 习题6 设实二次型 \(f(x_1,x_2,\cdots,x_n)= ...
- iOS-浅谈runtime运行时机制-runtime简单使用(转)
转自http://www.cnblogs.com/guoxiao/p/3583432.html 由于OC是运行时语言,只有在程序运行时,才会去确定对象的类型,并调用类与对象相应的方法.利用runtim ...
- Zero Copy
原文出处: http://www.ibm.com/developerworks/library/j-zerocopy/ 传统的I/O 使用传统的I/O程序读取文件内容, 并写入到另一个文件(或Sock ...
- 视频播放器之————JW Player参数详解
JW Player参数详解 1,安装 下载后,你可以得到一个例子,当用文本或HTML编辑器打开的时候,你可以发现swf是用一段短小的 javascript嵌入到页面上的.这个Javascript是Ge ...
- 如何正确的将J2ee项目部署到Tomcat
如何正确的将J2ee项目部署到Tomcat 1.打开配置文件(我的如下:C:\Program Files\Apache Software Foundation\Tomcat 7.0\conf\serv ...
- C#日常知识
常量: 定义常量:const; 条件运算符: 表达式1?表达式2:表达式3[如果正确则执行表达式2,不正确执行表达式3] (例如:int result; result = 5>4?100:200 ...
- Linux下I/O模型
Unix下共有五种I/O模型 1. 阻塞式I/O 2. 非阻塞式I/O 3. I/O复用(select和poll) 4. 信号驱动式I/O(SIGIO) 5. 异步I/O(POSIX的aio_系列函数 ...
- github文件上传及github pages博客搭建教程
一.与github建立连接 1.安装node.js和git 2.桌面新建文件夹[github],右键“git bash here” 3.注册github账号,新建仓库“new repository”, ...