几乎每个后端程序员在实际工作中或学习中都会用到spring,spring为我们创建了对象,摒弃了直接在代码中new一个对象的方法,其最大的特点就是:解耦合,可以说解耦合也是spring的一大特点。下面我们介绍spring创建bean的三种形式:
其中创建项目及新建类的过程省略,只保留主要文件。
第一种:通过构造函数来创建对象
首先新建一个person类,很简单的一个person类,只有set和get方法
-
package org.
example;
-
-
public
class
Person {
-
-
private
String name;
-
-
private
String age;
-
-
public
String
getName(
) {
-
return name;
-
}
-
-
public
void
setName(
String name) {
-
this.
name = name;
-
}
-
-
@Override
-
public
String
toString(
) {
-
return
"Person{" +
-
"name='" + name +
'\'' +
-
", age='" + age +
'\'' +
-
'}';
-
}
-
-
public
String
getAge(
) {
-
return age;
-
}
-
-
public
void
setAge(
String age) {
-
this.
age = age;
-
}
-
}
然后新建applicationContext.xml文档
<bean id = "person" class="org.example.Person">
最后再新建一个test类,可以用自带的
-
@Test
-
public void test2(){
-
ApplicationContext ctx =
new ClassPathXmlApplicationContext(
"/applicationContext.xml");
-
//1
-
Person person = (Person)ctx.getBean(
"person");
-
//2
-
Person person1 = ctx.getBean(
"person",Person.
class);
-
//3
-
Person person2 = ctx.getBean(Person.
class);
-
System.
out.println(person);
-
System.
out.println(person1);
-
System.
out.println(person2);
-
}
其中1,2,3分别代表三种获取bean对象的形式,执行该test类,这是第一种方法。
第二种方式:通过实例工厂
首先创建一个类ConnectionFactory。
-
package factoryBean;
-
-
import java.sql.Connection;
-
import java.sql.DriverManager;
-
import java.sql.SQLException;
-
-
public
class
ConnectionFactory {
-
-
public Connection
getConnection
(){
-
Connection
connection
=
null;
-
try {
-
Class.forName(
"com.mysql.jdbc.Driver");
-
connection = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/db_nacos?useSSL=false",
"root",
"123456");
-
}
catch (ClassNotFoundException e) {
-
e.printStackTrace();
-
}
catch (SQLException e) {
-
e.printStackTrace();
-
}
-
return connection;
-
}
-
-
}
然后applicationContext.xml中添加如下内容
-
<bean name="connectionFactory" class="factoryBean.ConnectionFactory"/>
-
<bean name="conn" factory-bean="connectionFactory" factory-method="getConnection"/>
然后再编写测试类
-
@Test
-
public
void
test3
(){
-
ApplicationContext
ctx
=
new
ClassPathXmlApplicationContext(
"/applicationContext.xml");
-
Connection
conn
= (Connection) ctx.getBean(
"conn");
-
System.out.println(conn);
-
}
直接运行结果即可,这是第二种创建实例工厂
第三种:创建静态工厂
首先创建类StaticConnectionFactory
-
package factoryBean;
-
-
import java.sql.Connection;
-
import java.sql.DriverManager;
-
import java.sql.SQLException;
-
-
public
class
StaticConnectionFactory {
-
-
public
static Connection
getConnection
(){
-
Connection
connection
=
null;
-
try {
-
Class.forName(
"com.mysql.jdbc.Driver");
-
connection = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/db_nacos",
"root",
"123456");
-
}
catch (ClassNotFoundException e) {
-
e.printStackTrace();
-
}
catch (SQLException e) {
-
e.printStackTrace();
-
}
finally {
-
}
-
return connection;
-
}
-
-
}
然后applicationContext.xml文件中添加如下内容
-
<!--spring创建静态工厂-->
-
<bean id="connection" class="factoryBean.StaticConnectionFactory" factory-method="getConnection">
</bean>
然后编写测试类
-
@Test
-
public
void
test4
(){
-
ApplicationContext
ctx
=
new
ClassPathXmlApplicationContext(
"/applicationContext.xml");
-
Connection
connection
= (Connection) ctx.getBean(
"connection");
-
System.out.println(connection);
-
}
运行即可。
以上就是spring创建bean的三种方式,供大家参考
转载:https://blog.csdn.net/qq_24972061/article/details/128139060
查看评论