Spring内部Bean利用set注入数据实现将DataSource交由Spring管理-----Spring框架

文章详细描述了如何在Spring框架中使用XML配置文件管理Bean,包括依赖注入(DI)OrderDao,OrderService,User,SimpleValueType以及MyDataSource对象的实例化和属性设置。
摘要由CSDN通过智能技术生成
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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),交由Spring管理该Bean的创建和与其他Bean对象之间的关系-->
    <bean id="OrderDao" class="com.powernode.spring6.dao.OrderDao"/>
    <bean id="OrderService" class="com.powernode.spring6.service.OrderService">
        <!--注入外部Bean,用REF(references引用)将外部的Bean注入Bean内-->
        <property name="orderDao" ref="OrderDao"/>
    </bean>
    <bean id="OS" class="com.powernode.spring6.service.OrderService">
        <property name="orderDao">
            <!--在property标签内的就叫内部Bean-->
            <bean class="com.powernode.spring6.dao.OrderDao"/>
        </property>
    </bean>
    <bean id="User" class="com.powernode.spring6.Bean.User">
        <!--如果使用简单数据类型的set方法,就需要使用value直接赋值了-->
        <property name="username" value="张三"/>
        <property name="age" value="20"/>
        <property name="password" value="123456"/>
    </bean>
    <bean id="SimpleValueType" class="com.powernode.spring6.Bean.SimpleValueType">
        <property name="age" value="20"/>
        <property name="ages" value="20"/>
        <property name="username" value="张三"/>
        <property name="flag" value="false"/>
        <property name="F" value="false"/>
        <property name="seasons" value="WINTER"/>
        <property name="clazz" value="java.lang.String"/>
        <property name="c" value="男"/>
        <property name="character" value="女"/>
        <property name="f" value="true"/>
        <!--日期最好用引用注入,用ref引入(格式过于复杂)-->
        <property name="birthday" value="Fri Aug 04 11:48:02 CST 2023"/>
    </bean>
    <bean id="MyDataSource" class="com.powernode.spring6.jdbc.MyDataSource">
        <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:13306/spring6"/>
        <property name="username" value="root"/>
        <property name="password" value="abc123"/>
    </bean>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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),交由Spring管理该Bean的创建和与其他Bean对象之间的关系-->
    <bean id="OrderDao" class="com.powernode.spring6.dao.OrderDao"/>
    <bean id="OrderService" class="com.powernode.spring6.service.OrderService">
        <!--注入外部Bean,用REF(references引用)将外部的Bean注入Bean内-->
        <property name="orderDao" ref="OrderDao"/>
    </bean>
    <bean id="OS" class="com.powernode.spring6.service.OrderService">
        <property name="orderDao">
            <!--在property标签内的就叫内部Bean-->
            <bean class="com.powernode.spring6.dao.OrderDao"/>
        </property>
    </bean>
    <bean id="User" class="com.powernode.spring6.Bean.User">
        <!--如果使用简单数据类型的set方法,就需要使用value直接赋值了-->
        <property name="username" value="张三"/>
        <property name="age" value="20"/>
        <property name="password" value="123456"/>
    </bean>
    <bean id="SimpleValueType" class="com.powernode.spring6.Bean.SimpleValueType">
        <property name="age" value="20"/>
        <property name="ages" value="20"/>
        <property name="username" value="张三"/>
        <property name="flag" value="false"/>
        <property name="F" value="false"/>
        <property name="seasons" value="WINTER"/>
        <property name="clazz" value="java.lang.String"/>
        <property name="c" value="男"/>
        <property name="character" value="女"/>
        <property name="f" value="true"/>
        <!--日期最好用引用注入,用ref引入(格式过于复杂)-->
        <property name="birthday" value="Fri Aug 04 11:48:02 CST 2023"/>
    </bean>
    <bean id="MyDataSource" class="com.powernode.spring6.jdbc.MyDataSource">
        <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:13306/spring6"/>
        <property name="username" value="root"/>
        <property name="password" value="abc123"/>
    </bean>
</beans>
package com.powernode.spring6.Bean;

public class User
{
    //String是简单类型,int也是简单类型
    private String username;
    private int age;
    private String password;
    public void setUsername(String username) {
        this.username = username;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    @Override
    public String toString() {
        return "User{" +
                "username='" + username + '\'' +
                ", age=" + age +
                ", password='" + password + '\'' +
                '}';
    }
}
package com.powernode.spring6.Bean;

public class User
{
    //String是简单类型,int也是简单类型
    private String username;
    private int age;
    private String password;
    public void setUsername(String username) {
        this.username = username;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    @Override
    public String toString() {
        return "User{" +
                "username='" + username + '\'' +
                ", age=" + age +
                ", password='" + password + '\'' +
                '}';
    }
}
package com.powernode.spring6.jdbc;

import javax.sql.DataSource;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.logging.Logger;
//数据源是用来获取连接对象的,所有的数据源都要实现java规范,javax.sql.DataSource
//凡是能提供connection对象的就是数据源
public class MyDataSource implements DataSource//把数据源交由Spring管理
{
    private String driver;
    private String url;
    private String username;
    private String password;

    @Override
    public String toString() {
        return "MyDataSource{" +
                "driver='" + driver + '\'' +
                ", url='" + url + '\'' +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }

    public void setDriver(String driver) {
        this.driver = driver;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    //获取数据库连接对象的时候需要4个信息,driver,url,username,password
    @Override
    public Connection getConnection() throws SQLException {
        return null;
    }

    @Override
    public Connection getConnection(String username, String password) throws SQLException {
        return null;
    }

    @Override
    public PrintWriter getLogWriter() throws SQLException {
        return null;
    }

    @Override
    public void setLogWriter(PrintWriter out) throws SQLException {

    }

    @Override
    public void setLoginTimeout(int seconds) throws SQLException {

    }

    @Override
    public int getLoginTimeout() throws SQLException {
        return 0;
    }

    @Override
    public Logger getParentLogger() throws SQLFeatureNotSupportedException {
        return null;
    }

    @Override
    public <T> T unwrap(Class<T> iface) throws SQLException {
        return null;
    }

    @Override
    public boolean isWrapperFor(Class<?> iface) throws SQLException {
        return false;
    }
}
package com.powernode.spring6.jdbc;

import javax.sql.DataSource;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.logging.Logger;
//数据源是用来获取连接对象的,所有的数据源都要实现java规范,javax.sql.DataSource
//凡是能提供connection对象的就是数据源
public class MyDataSource implements DataSource//把数据源交由Spring管理
{
    private String driver;
    private String url;
    private String username;
    private String password;

    @Override
    public String toString() {
        return "MyDataSource{" +
                "driver='" + driver + '\'' +
                ", url='" + url + '\'' +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }

    public void setDriver(String driver) {
        this.driver = driver;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    //获取数据库连接对象的时候需要4个信息,driver,url,username,password
    @Override
    public Connection getConnection() throws SQLException {
        return null;
    }

    @Override
    public Connection getConnection(String username, String password) throws SQLException {
        return null;
    }

    @Override
    public PrintWriter getLogWriter() throws SQLException {
        return null;
    }

    @Override
    public void setLogWriter(PrintWriter out) throws SQLException {

    }

    @Override
    public void setLoginTimeout(int seconds) throws SQLException {

    }

    @Override
    public int getLoginTimeout() throws SQLException {
        return 0;
    }

    @Override
    public Logger getParentLogger() throws SQLFeatureNotSupportedException {
        return null;
    }

    @Override
    public <T> T unwrap(Class<T> iface) throws SQLException {
        return null;
    }

    @Override
    public boolean isWrapperFor(Class<?> iface) throws SQLException {
        return false;
    }
}
package com.powernode.spring6.Bean;

import java.util.Date;

public class SimpleValueType
{
    private int age;
    private Integer ages;
    private boolean flag;
    private Boolean F;
    private char c;
    private Character character;
    private Seasons seasons;
    private String username;
    private Class clazz;
    private Date birthday;

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
    @Override
    public String toString() {
        return "SimpleValueType{" +
                "age=" + age +
                ", ages=" + ages +
                ", flag=" + flag +
                ", F=" + F +
                ", c=" + c +
                ", character=" + character +
                ", seasons=" + seasons +
                ", username='" + username + '\'' +
                ", clazz=" + clazz +
                ", birthday=" + birthday +
                '}';
    }
    public void setAge(int age) {
        this.age = age;
    }

    public void setAges(Integer ages) {
        this.ages = ages;
    }

    public void setFlag(boolean flag) {
        this.flag = flag;
    }

    public void setF(Boolean f) {
        F = f;
    }

    public void setC(char c) {
        this.c = c;
    }

    public void setCharacter(Character character) {
        this.character = character;
    }

    public void setSeasons(Seasons seasons) {
        this.seasons = seasons;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setClazz(Class clazz) {
        this.clazz = clazz;
    }
}
package com.powernode.spring6.Bean;

import java.util.Date;

public class SimpleValueType
{
    private int age;
    private Integer ages;
    private boolean flag;
    private Boolean F;
    private char c;
    private Character character;
    private Seasons seasons;
    private String username;
    private Class clazz;
    private Date birthday;

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
    @Override
    public String toString() {
        return "SimpleValueType{" +
                "age=" + age +
                ", ages=" + ages +
                ", flag=" + flag +
                ", F=" + F +
                ", c=" + c +
                ", character=" + character +
                ", seasons=" + seasons +
                ", username='" + username + '\'' +
                ", clazz=" + clazz +
                ", birthday=" + birthday +
                '}';
    }
    public void setAge(int age) {
        this.age = age;
    }

    public void setAges(Integer ages) {
        this.ages = ages;
    }

    public void setFlag(boolean flag) {
        this.flag = flag;
    }

    public void setF(Boolean f) {
        F = f;
    }

    public void setC(char c) {
        this.c = c;
    }

    public void setCharacter(Character character) {
        this.character = character;
    }

    public void setSeasons(Seasons seasons) {
        this.seasons = seasons;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setClazz(Class clazz) {
        this.clazz = clazz;
    }
}
import com.powernode.spring6.Bean.SimpleValueType;
import com.powernode.spring6.Bean.User;
import com.powernode.spring6.jdbc.MyDataSource;
import com.powernode.spring6.service.OrderService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.Date;

public class SpringDITest
{
    public static void main(String[] args) {
        System.out.println(new Date());
    }
    @Test
    public void TestSetDI()
    {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("set-di.xml");
        OrderService OrderService = applicationContext.getBean("OrderService", OrderService.class);
        OrderService.generate();
        OrderService = applicationContext.getBean("OS",OrderService.class);
        OrderService.generate();
        User user = applicationContext.getBean("User", User.class);
        System.out.println(user);
        SimpleValueType simpleValueType = applicationContext.getBean("SimpleValueType",SimpleValueType.class);
        System.out.println(simpleValueType);
        applicationContext = new ClassPathXmlApplicationContext("set-di.xml");
        MyDataSource MyDataSource = applicationContext.getBean("MyDataSource", MyDataSource.class);
        System.out.println(MyDataSource);
    }
}
import com.powernode.spring6.Bean.SimpleValueType;
import com.powernode.spring6.Bean.User;
import com.powernode.spring6.jdbc.MyDataSource;
import com.powernode.spring6.service.OrderService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.Date;

public class SpringDITest
{
    public static void main(String[] args) {
        System.out.println(new Date());
    }
    @Test
    public void TestSetDI()
    {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("set-di.xml");
        OrderService OrderService = applicationContext.getBean("OrderService", OrderService.class);
        OrderService.generate();
        OrderService = applicationContext.getBean("OS",OrderService.class);
        OrderService.generate();
        User user = applicationContext.getBean("User", User.class);
        System.out.println(user);
        SimpleValueType simpleValueType = applicationContext.getBean("SimpleValueType",SimpleValueType.class);
        System.out.println(simpleValueType);
        applicationContext = new ClassPathXmlApplicationContext("set-di.xml");
        MyDataSource MyDataSource = applicationContext.getBean("MyDataSource", MyDataSource.class);
        System.out.println(MyDataSource);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值