2、设计实现一个Bean,使用静态工厂方式注册该Bean,并通过ApplicationContext验证它。 3、设计实现一个Bean,使用实例工厂方式注册该Bean,并通过ApplicationContext验证它。 4、设计实现一个Bean,使用构造注入方式注册该Bean,并通过ApplicationContext验证它。 5、设计实现一个Bean,使用设值注入方式注册该Bean,并通过ApplicationContext验证它。 6、设计实现两个Bean,其中一个的scope为singleton,另一个为prototype,并通过ApplicationContext验证它们的实例创建scope。
时间: 2024-02-05 16:11:13 浏览: 45
2、使用静态工厂方式注册Bean:
```java
public class MyBean {
private String name;
public MyBean(String name) {
this.name = name;
}
public void sayHello() {
System.out.println("Hello, " + name + "!");
}
}
public class MyBeanFactory {
public static MyBean createMyBean(String name) {
return new MyBean(name);
}
}
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
MyBean myBean = (MyBean) context.getBean("myBean");
myBean.sayHello();
}
<!-- applicationContext.xml 配置文件 -->
<bean id="myBean" class="com.example.MyBeanFactory" factory-method="createMyBean">
<constructor-arg value="张三"/>
</bean>
```
3、使用实例工厂方式注册Bean:
```java
public class MyBean {
private String name;
public MyBean(String name) {
this.name = name;
}
public void sayHello() {
System.out.println("Hello, " + name + "!");
}
}
public class MyBeanFactory {
public MyBean createMyBean(String name) {
return new MyBean(name);
}
}
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
MyBean myBean = (MyBean) context.getBean("myBean");
myBean.sayHello();
}
<!-- applicationContext.xml 配置文件 -->
<bean id="myBeanFactory" class="com.example.MyBeanFactory"/>
<bean id="myBean" factory-bean="myBeanFactory" factory-method="createMyBean">
<constructor-arg value="李四"/>
</bean>
```
4、使用构造注入方式注册Bean:
```java
public class MyBean {
private String name;
public MyBean(String name) {
this.name = name;
}
public void sayHello() {
System.out.println("Hello, " + name + "!");
}
}
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
MyBean myBean = (MyBean) context.getBean("myBean");
myBean.sayHello();
}
<!-- applicationContext.xml 配置文件 -->
<bean id="myBean" class="com.example.MyBean">
<constructor-arg value="王五"/>
</bean>
```
5、使用设值注入方式注册Bean:
```java
public class MyBean {
private String name;
public void setName(String name) {
this.name = name;
}
public void sayHello() {
System.out.println("Hello, " + name + "!");
}
}
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
MyBean myBean = (MyBean) context.getBean("myBean");
myBean.sayHello();
}
<!-- applicationContext.xml 配置文件 -->
<bean id="myBean" class="com.example.MyBean">
<property name="name" value="赵六"/>
</bean>
```
6、创建singleton和prototype Bean:
```java
public class SingletonBean {
private String name;
public SingletonBean(String name) {
this.name = name;
}
public void sayHello() {
System.out.println("Hello, " + name + "!");
}
}
public class PrototypeBean {
private String name;
public PrototypeBean(String name) {
this.name = name;
}
public void sayHello() {
System.out.println("Hello, " + name + "!");
}
}
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
SingletonBean singleton1 = (SingletonBean) context.getBean("singletonBean");
SingletonBean singleton2 = (SingletonBean) context.getBean("singletonBean");
PrototypeBean prototype1 = (PrototypeBean) context.getBean("prototypeBean");
PrototypeBean prototype2 = (PrototypeBean) context.getBean("prototypeBean");
System.out.println(singleton1 == singleton2); // true
System.out.println(prototype1 == prototype2); // false
}
<!-- applicationContext.xml 配置文件 -->
<bean id="singletonBean" class="com.example.SingletonBean" scope="singleton">
<constructor-arg value="单例Bean"/>
</bean>
<bean id="prototypeBean" class="com.example.PrototypeBean" scope="prototype">
<constructor-arg value="原型Bean"/>
</bean>
```