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 ...
随机推荐
- 2016年12月21日 星期三 --出埃及记 Exodus 21:16
2016年12月21日 星期三 --出埃及记 Exodus 21:16 "Anyone who kidnaps another and either sells him or still h ...
- 【iOS】The differences between Class Extension and Header File 类扩展与头文件的区别
. As the name suggests, they extend the class. A class continuation is another name. The class exten ...
- ArrayList代码示例
package com.shushine.framework.第七章Java标准类库;import java.util.ArrayList;/** * * <p> * 描述该类情况 {@l ...
- Linux 日常命令
命令 介绍说明 pwd 打印出当前所在目录 mkdir 创建一个目录 rmdir 删除一个目录 rm 删除文件或目录 -r:删除目录时必须加上 cp 复制一个文件或目录 -r:复制 ...
- 后勤模块数据源的增量队列(Delta-Queue)三种更新模式(Update Mode)
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- Response.BinaryWrite()方法输出二进制图像
protected void Page_Load(object sender, EventArgs e) { FileStream fs = new FileStream(Server.MapPath ...
- LTE Module User Documentation(翻译12)——X2切换(X2-based handover)
LTE用户文档 (如有不当的地方,欢迎指正!) 18 X2-based handover 正如 3GPP 定义的,切换是改变用户服务小区的连接方式的过程.这一过程中涉及的两个基站通常称为源基站和目 ...
- c语言指针占几个字节
指针即为地址,指针几个字节跟语言无关,而是跟系统的寻址能力有关,譬如以前是16为地址,指针即为2个字节,现在一般是32位系统,所以是4个字节,以后64位,则就为8个字节. 可以在自己的电脑上测试下: ...
- HBase之集群状态
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.*; import org.apache.had ...
- Android 如何解决数据库多线程锁的问题
防止多个线程又是读取又是写入 网上找到的方法: 对于这样的问题,解决的办法就是keep single sqlite connection,保持单个SqliteOpenHelper实例,同时对所有数据库 ...