【spring bean】bean的配置和创建方式

时间:2023-03-09 08:39:01
【spring  bean】bean的配置和创建方式

---恢复内容开始---

项目结构如下:

【spring  bean】bean的配置和创建方式

lib如下:

【spring  bean】bean的配置和创建方式

1.首先建立SayHell.java接口

 package com.it.sxd;

 public interface SayHell {
public void sayHello();
}

2.下面有它的两个实现类

SayHelloImpl.java

 package com.it.sxd;

 public class SayHelloImpl implements SayHell {

     @Override
public void sayHello() {
System.out.println("hello");
} }

SayHelloImpl2.java

 package com.it.sxd;

 public class SayHelloImpl2 implements SayHell {
private String message; public SayHelloImpl2() {
this.message = "无参的构造器";
} public SayHelloImpl2(String message) {
super();
this.message = message;
} @Override
public void sayHello() {
System.out.println(message);
} }

还有一个静态工厂SayHelloFactory.java

 package com.it.sxd;

 public class SayHelloFactory {

     public static SayHell  sayhell(String message){
return new SayHelloImpl2(message);
} public static void sayAmount(){
double b= 9/3;
System.out.println(b);
} }

3.配置文件hello.xml【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-3.0.xsd"> <!--测试1 :bean就是Ioc容器初始化。装配,管理的对象,除此之外,bean与应用程序中的对象没有任何区别, id 表示你这个组件的名字,class表示组件类,也就是位置 -->
<bean id="hello1" class="com.it.sxd.SayHelloImpl">
</bean> <!--测试2 无参的构造方法 使用标签alisa起别名 -->
<bean id="hello3" class="com.it.sxd.SayHelloImpl2">
</bean>
<alias name="hello3" alias="alisa3"/> <!-- 测试2 有参数的构造方法 使用name,给多个值起别名-->
<bean id="hello2" name="alisa1,alisa2" class="com.it.sxd.SayHelloImpl2">
<constructor-arg index="0" value="hello有参构造方法"></constructor-arg><!-- 默认的为构造方法传值,index代表第几个参数,0代表第一个,value就是给构造器传递的值 -->
</bean> <!-- 测试3 静态工厂创建bean factory-method就代表初始化这个class类的时候,就执行了这个类中的"sayhell"方法,并且要求这个方法必须是static -->
<bean id="hello4" class="com.it.sxd.SayHelloFactory" factory-method="sayhell">
<constructor-arg type="java.lang.String" value="静态工厂创建bean"></constructor-arg><!-- 为这个sayhell方法传递参数,参数类型是String类型,值是value -->
</bean> <!-- 测试3 静态工厂创建bean ,init-method初始化执行的方法 -->
<bean id="hello5" class="com.it.sxd.SayHelloFactory" init-method="sayAmount">
</bean> </beans>

4.分别测试一下上面的两个实现类的对不同bean的组合配置

SayHelloTest.java 最基本的测试 ,对应SayHelloImpl.java

 package com.it.sxd;

 import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext; public class SayHelloTest { @Test
public void testSayHello(){
//System.out.println(SayHelloTest.class.getResource(""));
//1.读取hello。xml配置文件,实例化一个Ioc容器
ApplicationContext context = new FileSystemXmlApplicationContext("resources/hello.xml");
//2.从Ioc容器中获取ID为"hello1"的bean ,此处是“面向接口编程 而不是面向实现编程”
SayHell sayHell = context.getBean("hello1",SayHell.class);
//3.实现功能
sayHell.sayHello();
} }

SayHelloTest2.java 有无参数的构造方法 对应SayHelloImpl2.java

 package com.it.sxd;

 import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext; public class SayHelloTest2 { @Test
public void testSayHello() {
ApplicationContext context = new FileSystemXmlApplicationContext("resources/hello.xml");
SayHell syahell = context.getBean("hello2",SayHell.class);//getBean()方法传进来两个参数 参数1:配置文件中bean的id 参数2:SayHell.class就是这个bean返回的结果强转为SayHell类型的
syahell.sayHello(); SayHell sayhell2 = (SayHell) context.getBean("alisa3");//如果geyBean()方法不传进来第二个参数,那就需要自己进行强转
sayhell2.sayHello(); String [] attr = context.getAliases("hello2");//getAliases() 获取id为hello2的bean的别名
for (String string : attr) {
System.out.println(string);
}
} }

SayHelloTest3.java

 package com.it.sxd;

 import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext; public class SayHelloTest3 {
@Test
public void sayHello(){
//1。通过加载配置文件来实例化一个Ioc容器,
ApplicationContext context = new FileSystemXmlApplicationContext("resources/hello.xml");
//2.从Ioc容器得到对应的bean
SayHell sayhell = context.getBean("hello4",SayHell.class);
//3.执行bean的方法
sayhell.sayHello(); SayHelloFactory sa = (SayHelloFactory) context.getBean("hello5"); }
}

运行的时候 右键 JUnit运行

---恢复内容结束---

---恢复内容结束---