spring mysql注入_Spring依赖注入之复杂类型的注入

1、List、Set、Map、Properties集合注入

实体类

public class User {

private List hobit;

private Set subject;

private Map score;

private Properties db;

public User() {

}

public User(List hobit, Set subject, Map score, Properties db) {

this.hobit = hobit;

this.subject = subject;

this.score = score;

this.db = db;

}

public List getHobit() {

return hobit;

}

public void setHobit(List hobit) {

this.hobit = hobit;

}

public Set getSubject() {

return subject;

}

public void setSubject(Set subject) {

this.subject = subject;

}

public Map getScore() {

return score;

}

public void setScore(Map score) {

this.score = score;

}

public Properties getDb() {

return db;

}

public void setDb(Properties db) {

this.db = db;

}

@Override

public String toString() {

return "User{" +

"hobit=" + hobit +

", subject=" + subject +

", score=" + score +

", db=" + db +

'}';

}

}

数据库配置文件(db.properties)

driverClassName = com.mysql.jdbc.Driver

url = jdbc:mysql://localhost:3306/eesy

username = root

password = root

配置文件(applicationContext.xml)

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:p="http://www.springframework.org/schema/p"

xmlns:util="http://www.springframework.org/schema/util"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-4.0.xsd

http://www.springframework.org/schema/util

http://www.springframework.org/schema/util/spring-util-4.0.xsd">

电影

音乐

阅读

阅读

Chinese

Math

English

English

测试类

public class TestDI {

@Test

public void test() {

ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");

User user = ac.getBean("user", User.class);

System.out.println(user);

}

}

测试结果

User{hobit=[电影, 音乐, 阅读, 阅读], subject=[Chinese, Math, English], score={Chinese=80.0, Math=86.0, English=90.0}, db={password=root, url=jdbc:mysql://localhost:3306/eesy, driverClassName=com.mysql.jdbc.Driver, username=root}}

2、引用的方式注入集合

实体类

public class User {

private List hobit;

private Set subject;

private Map score;

private Properties db;

public User() {

}

public User(List hobit, Set subject, Map score, Properties db) {

this.hobit = hobit;

this.subject = subject;

this.score = score;

this.db = db;

}

public List getHobit() {

return hobit;

}

public void setHobit(List hobit) {

this.hobit = hobit;

}

public Set getSubject() {

return subject;

}

public void setSubject(Set subject) {

this.subject = subject;

}

public Map getScore() {

return score;

}

public void setScore(Map score) {

this.score = score;

}

public Properties getDb() {

return db;

}

public void setDb(Properties db) {

this.db = db;

}

@Override

public String toString() {

return "User{" +

"hobit=" + hobit +

", subject=" + subject +

", score=" + score +

", db=" + db +

'}';

}

}

数据库配置文件(db.properties)

driverClassName = com.mysql.jdbc.Driver

url = jdbc:mysql://localhost:3306/eesy

username = root

password = root

配置文件(applicationContext.xml)

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:p="http://www.springframework.org/schema/p"

xmlns:util="http://www.springframework.org/schema/util"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-4.0.xsd

http://www.springframework.org/schema/util

http://www.springframework.org/schema/util/spring-util-4.0.xsd">

电影

音乐

阅读

阅读

Chinese

Math

English

English

scott

tiger

测试类

public class TestDI {

@Test

public void test() {

ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");

User user = ac.getBean("user", User.class);

System.out.println(user);

}

}

测试结果

User{hobit=[电影, 音乐, 阅读, 阅读], subject=[Chinese, Math, English], score={Chinese=80.0, Math=86.0, English=90.0}, db={password=root, url=jdbc:mysql://localhost:3306/eesy, driverClassName=com.mysql.jdbc.Driver, username=root}}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值