一  配置xml方式:扫描com包下的bean

<?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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com"/> </beans>

  com包下一个类:

@Component
@PropertySource("p.properties")
public class Person { @Value("${name}")
public String name; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
}
}

 resource下 p.properties中的文件内容:

name=hahadaxiao

  main函数:

public static void main(String[] args){
ApplicationContext context = new ClassPathXmlApplicationContext("a.xml");
Person p = context.getBean("person",Person.class);
System.out.println(p.name);
}

  打印:

hahadaxiao

二 注解方式

com包下存在下面这个类,默认扫描Config1所在的包下的Bean

@Configuration
@ComponentScan
public class Config1 { }

 

@Component
@PropertySource("p.properties")
public class Person { @Value("${name}")
public String name; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
}
}

  

public static void main(String[] args){
ApplicationContext context = new AnnotationConfigApplicationContext(Config1.class);
Person p = context.getBean("person",Person.class);
System.out.println(p.name);
}

  打印:

hahadaxiao

Spring基础(9) : 自动扫描的更多相关文章

  1. Spring基础知识

    Spring基础知识 利用spring完成松耦合 接口 public interface IOutputGenerator { public void generateOutput(); } 实现类 ...

  2. Spring Boot实战(1) Spring基础

    1. Spring基础配置 Spring框架本身有四大原则: 1) 使用POJO进行轻量级和最小侵入式开发 2) 通过依赖注入和基于接口编程实现松耦合 3) 通过AOP和默认习惯进行声明式编程 4) ...

  3. spring基础整理

    spring基础教程:https://www.tutorialspoint.com/spring/spring_overview.htm 注入实例 <bean id="" c ...

  4. Spring 基础知识

    Spring架构简单描述 原文:https://www.shiyanlou.com/courses/document/212 Spring 概述 1. Spring 是什么 Spring是一个开源的轻 ...

  5. Spring基础配置

    从毕业到现在我一直从事Android开发,但是对JavaEE一直念念不忘,毕业校招的时候,一个礼拜拿了三个offer,岗位分别是Android.JavaEE和JavaSE,后来觉得Android比较简 ...

  6. Spring基础系列--AOP织入逻辑跟踪

    原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9619910.html 其实在之前的源码解读里面,关于织入的部分并没有说清楚,那些前置.后 ...

  7. 第65节:Java后端的学习之Spring基础

    Java后端的学习之Spring基础 如果要学习spring,那么什么是框架,spring又是什么呢?学习spring中的ioc和bean,以及aop,IOC,Bean,AOP,(配置,注解,api) ...

  8. Spring基础系列-AOP源码分析

    原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9560803.html 一.概述 Spring的两大特性:IOC和AOP. AOP是面向切 ...

  9. Spring基础系列-Spring事务不生效的问题与循环依赖问题

    原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9476550.html 一.提出问题 不知道你是否遇到过这样的情况,在ssm框架中开发we ...

  10. @Autowired注解和启动自动扫描的三种方式(spring bean配置自动扫描功能的三种方式)

    前言: @Autowired注解代码定义 @Target({ElementType.CONSTRUCTOR, ElementType.FIELD, ElementType.METHOD, Elemen ...

随机推荐

  1. PostgresSQL使用Copy命令能大大提高数据导入速度

    最近在做会员系统,其中会员系统有一份企业信息初始化的数据,需要从SQL Server数据库导入到PostgreSQL,单表的数据近30万.最开始的方案是在SQL Server上生成insert int ...

  2. deeplearning 源码收集

    Theano – CPU/GPU symbolic expression compiler in python (from MILA lab at University of Montreal) To ...

  3. DataFrame 操作

    # 删除td数据框中的 指定列 td.drop(columns=['ship-city'],axis=1,inplace=True) #删除指定列 并且修改掉原始数据 # 删除td数据框中的 最后一列 ...

  4. 一些浩辰设置及它的bug?

    gcad执行快捷键有问题?尝试修改Setvar("autocompletemode", "19");原因是’输入按键时显示建议列表’这个项打钩了,这里首先捕捉的 ...

  5. JavaScript中标识符的命名

    JavaScript中的标识符的命名有以下规则: 由字母.数字.$._组成 以字母.$._开头 不可以使用保留字!!! 要有意义!!!!!!! 标识符的命名规范: 1.驼峰命名法 除标识符的第一个单词 ...

  6. npm 包 升降版本

    今天用vue-awesome-swiper最新版本遇到些问题,需要调整到2.6.7版本.记录以下. 查看vue-awesome-swiper版本 npm list vue-awesome-swiper ...

  7. PowerDesigner生成OOM时类名属性名转换

    Examples Script 1: Convert a name into a class code (JAVA naming convention)转换类名 .foreach_part(%Name ...

  8. Windows下Mongodb安装及配置

    安装文件:MongoDB-win32-x86_64-2008plus-ssl-3.2.6-signed.msi 电脑配置:win7 64位 mongodb的安装很简单,设置好安装路径后,一直Next直 ...

  9. Python staticmethod classmethod 普通方法 类变量 实例变量 cls self 概念与区别

    类变量 1.需要在一个类的各个对象间交互,即需要一个数据对象为整个类而非某个对象服务. 2.同时又力求不破坏类的封装性,即要求此成员隐藏在类的内部,对外不可见. 3.有独立的存储区,属于整个类.   ...

  10. Linux 服务器部署 PgAdmin 4 Server

    PostgreSQL 使用率越来越高,但是好用的图形化软件,诸如 Navicat.DataGrip.HeidiSQL 都是客户端软件,对于访问控制来说,比起 MySQL 的 phpMyAdmin 更加 ...