博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring 配置bean的方法及依赖注入发方式
阅读量:4584 次
发布时间:2019-06-09

本文共 2907 字,大约阅读时间需要 9 分钟。

Bean 的配置方式:通过全类名(反射)、通过工厂方法(静态工厂方法 & 实例工厂方法)、FactoryBean

这里依据全类名配置bean

<bean id="helloWord" class="com.spring.HelloWord">

 <property name="userName" value="springsss"></property>
</bean>

依赖注入发方式:

属性注入:

applicationContext.xml配置文件为:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 配置bean -->
<bean id="helloWord" class="com.spring.HelloWord">
 <property name="userName" value="springsss"></property>
</bean>
</beans>

package com.spring;

public class HelloWord {
private String userName;
public void setUserName(String userName) {
this.userName = userName;
}
public void hello() {
System.out.println("hello:" + userName);
}
public HelloWord(){
System.out.println("construct!!!!!!!!!!");
}
}

package com.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
/**
* 创建对象及为对象赋值交给spring完毕
*/
// HelloWord helloWord = new HelloWord();
// helloWord.setUserName("hello");
// helloWord.hello();
//1.创建spring IOC容器
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
//2从容器中获得Bean
HelloWord helloWord = (HelloWord) ctx.getBean("helloWord");
//3.调用方法
helloWord.hello();
}
}

输出结果为:

construct!!!!!!!!!!

hello:springsss

构造器注入:

applicationContext.xml文件为

<?xml version="1.0" encoding="UTF-8"?

>

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 配置bean -->
<bean id="car" class="com.spring.Car">
<constructor-arg value="green"></constructor-arg>
<constructor-arg value="22"></constructor-arg>
</bean>
</beans>

public class Car {

 private String name;
 private String color;
 private int num;
public Car(String color, int num) {
super();
this.color = color;
this.num = num;
}
@Override
public String toString() {
return "Car [name=" + name + ", color=" + color + ", num=" + num + "]";
}
}

package com.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
//1.创建spring IOC容器
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
Car car = (Car) ctx.getBean("car");
System.out.println(car.toString());
}
}

输出结果为:

Car [name=null, color=green, num=22]

posted on
2017-08-16 12:23 阅读(
...) 评论(
...)

转载于:https://www.cnblogs.com/mthoutai/p/7372782.html

你可能感兴趣的文章
JAVA基础学习之 Map集合、集合框架工具类Collections,Arrays、可变参数、List和Set集合框架什么时候使用等(4)...
查看>>
用Python学分析 - 单因素方差分析
查看>>
2018个人年终总结
查看>>
[编辑排版]小技巧---markdown 转 richText
查看>>
JSON_UNESCAPED_UNICODE
查看>>
bug解决思路
查看>>
Oracle没有WM_CONCAT函数的解决办法
查看>>
消息中间件——RabbitMQ(四)命令行与管控台的基本操作!
查看>>
Eclipse 写代码是自动重启服务
查看>>
3.8 spring - AbstractBeanDefinition 介绍
查看>>
如何在Visual Studio里面查看程序的汇编代码?
查看>>
解决IE11只能用管理员身份运行的问题
查看>>
android学习-LocationManager(一)-
查看>>
Linux安装单机solr
查看>>
dos alias/cname address
查看>>
NAT模式实现局域网物理机与虚拟机的互通访问
查看>>
cygwin下用arm-xscale-linux-gnueabi交叉编译libcgi
查看>>
delphi中WMI的使用(网卡是否接入)
查看>>
转载:面试:----电商项目中比较难得问题
查看>>
js弹出遮罩层
查看>>