【spring】之xml和Annotation,Bean注入的方式
基于xml形式Bean注入
@Data
@AllArgsConstructor
@NoArgsConstructor
public class PersonBean {
private Integer id;
private String name;
private String address;
}
<bean class="com.luna.annotation.PersonBean" id="personBean">
<property name="name" value="张三"/>
<property name="address" value="上海"/>
<property name="id" value=""/>
</bean>
//基于xml完成数据Bean注入
@Test
public void test(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
PersonBean personBean = (PersonBean) applicationContext.getBean("personBean");
System.out.println(personBean.getAddress());
}
基于Annotation形式Bean注入
@Configuration//告诉spring这是个配置类
public class PersonBeanConfig { @Bean(value = "person")//默认情况下使用方法名作为属性名
public PersonBean personBean(){
return new PersonBean(,"李四","北京");
} }
//基于纯注解完成数据注入
@Test
public void test(){
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(PersonBeanConfig.class);
PersonBean personBean = applicationContext.getBean(PersonBean.class);//类型
System.out.println(personBean.getAddress());
PersonBean person = (PersonBean)applicationContext.getBean("person");//属性名
System.out.println(person.getAddress());
String[] names= applicationContext.getBeanNamesForType(PersonBean.class);//查看在容器中当前Bean的属性名
for(String name:names){
System.out.println(name);
}
}
【spring】之xml和Annotation,Bean注入的方式的更多相关文章
- 【spring】spring源码阅读之xml读取、bean注入(BeanFactory)
前言 此源码其实是在4月中旬就看了,而且当初也写了一份word文档,但不打算直接把word发上来.还是跟着以前的笔记.跟踪代码边看边写吧. 其实当初看源码的理由很简单,1.才进新公司,比较有空闲.2. ...
- Spring 通过XML配置装配Bean
使用XML装配Bean需要定义对于的XML,需要引入对应的XML模式(XSD)文件,这些文件会定义配置Spring Bean的一些元素,简单的配置如下: <?xml version=" ...
- 聊聊 Spring 的 XML Schema 扩展机制的使用方式
前言 在当前Java生态,Spring算的上是最核心的框架,所有的开发组件想要得到大范围更便捷的使用,都要和Spring进行整合,比如我们熟知的Mybatis.Dubbo等,以及内部封装的各类组件包括 ...
- 【Spring】Spring中的Bean - 5、Bean的装配方式(XML、注解(Annotation)、自动装配)
Bean的装配方式 简单记录-Java EE企业级应用开发教程(Spring+Spring MVC+MyBatis)-Spring中的Bean 文章目录 Bean的装配方式 基于XML的装配 基于注解 ...
- [spring]Bean注入——在XML中配置
Bean注入的方式有两种: 一.在XML中配置 属性注入 构造函数注入 工厂方法注入 二.使用注解的方式注入@Autowired,@Resource,@Required 本文首先讲解在XML中配置的注 ...
- spring中bean配置和bean注入
1 bean与spring容器的关系 Bean配置信息定义了Bean的实现及依赖关系,Spring容器根据各种形式的Bean配置信息在容器内部建立Bean定义注册表,然后根据注册表加载.实例化Bean ...
- 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring基于XML装配Bean
Bean 的装配可以理解为依赖关系注入,Bean 的装配方式也就是 Bean 的依赖注入方式.Spring 容器支持多种形式的 Bean 的装配方式,如基于 XML 的 Bean 装配.基于 Anno ...
- Spring框架系列(二)--装配和注入Bean
企业日常开发中,几乎都是Spring系的框架,无论是SSM.还是现在大火的SpringBoot+JPA/MyBatis,使用最大的目的就是简化开发 基本模块: 核心容器:Beans.Core.Cont ...
- org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager' defined in class path resource [spring/applicationContext-service.xml]: Cannot resolve refer
<!-- aop --> <aop:config> <aop:pointcut expression="execution(* com.zsn.Service. ...
随机推荐
- stylus笔记(二)
1.方法 函数 Stylus强大之处就在于其内置的语言函数定义.其定义与混入(mixins)一致:却可以返回值. 默认参数 可选参数往往有个默认的给定表达.在Stylus中,我们甚至可以超越默认参数 ...
- Petrozavodsk Winter Camp, Day 8, 2014, Mosaic
给你三个数字a,b,c,让你用1-m的数字凑出来 结论:有2个1和2个2肯定凑不出来,然后就搜索 #include <bits/stdc++.h> using namespace std; ...
- python自动化测试入门篇-postman
接口测试基础-postman 常用的接口有两种:webservice接口和http api接口. Webservice接口是走soap协议通过http传输,请求报文和返回报文都是xml格式. http ...
- 1022. Sum of Root To Leaf Binary Numbers从根到叶的二进制数之和
网址:https://leetcode.com/problems/sum-of-root-to-leaf-binary-numbers/ 递归调用求和,同时注意%1000000007的位置 /** * ...
- 4.清除cookie操作
1.cookies Cookie是储存在用户本地终端上的数据 帮助 Web 站点保存有关访问者的信息,方便用户的访问.如记住用户名密码实现自动登录 from selenium import webdr ...
- git创建分支提交到远程分支步骤
git创建分支提交到远程分支步骤 step1,在本地新建分支() git branch newbranch step2:把本地分支push到远程 git push origin newbranch s ...
- Python—列表操作
列表 列表的使用: 列表是可变数据类型,因此列表的方法,都是直接修改列表原始值 list = ["panda","chimpanzee","zebra ...
- Python测试框架之Unittest梳理
1. 2.
- MySQL:数据查询
数据查询 一.基本查询语句 1.语法:写一行 select{*<字段列表>}//查询的字段,多个字段用逗号分开 from<表1>,<表2>…//数据表名 {//可选 ...
- css的性质
css两个性质: 1.继承性 2.层叠行(选择器的一种选择能力,谁的权重大就选谁) A.选不中,走继承性,(font系列.color.text系列)权重是0 a)有多个父级都设置了这样的样式 走就 ...