目录
bean参数的注入(简单数据的注入)
基本类型的数据和字符串
在配置文件applicationContext-DI1.xml
注入简单的数据,例如String和一些基本数据类型
<bean id="msgBean" class="com.bean.MsgBean">
<property name="title">
<value>夜宵吃什么</value>
</property>
<property name="length" value="鱼香肉丝"></property>
</bean>
<bean id="msgBean2" class="com.bean.MsgBean">
<property name="title">
<null/>
</property>
<property name="length" value="null"></property>
</bean>
分别提供了三种注入方式
- 在property标签行内嵌入value属性:例如上面代码块的第5行
- 在property标签内嵌入value标签:例如上面代码块的第2行-第4行
- 在property标签内嵌入null标签:例如上面代码块的第8行-第10行
注入集合
通过<list/>
,<set/>
,<map/>
及<props/>
元素可以定义和设置与Java类型中对应List,Set,Map及Properties的属性值.
bean参数的注入(集合数据的注入)
Java中的List,对应配置文件中的标记
<property name="friends">
<list>
<value>逼哥</value>
<value>马哥</value>
<value>耀儿</value>
<value>肖二狗</value>
</list>
</property>
Java中的Set,对应配置文件中的标记
<property name="friends">
<set>
<value>逼哥</value>
<value>马哥</value>
<value>耀儿</value>
<value>肖二狗</value>
</set>
</property>
Java中的Map注入
<property name="phones">
<map>
<!--这样会出错-->
<!--<entry>
<key>马哥</key>
<value>17608348340</value>
</entry>-->
<entry key="马哥" value="17608348340"></entry>
<entry key="王牛逼">
<value>17608348340</value>
</entry>
</map>
</property>
注入集合-Properties
注入Properties集合
Java中的Peoperties对应配置文件中的标记
例如:
<property name="delFriends">
<props>
<!--只能写key-->
<prop key="小马哥1">111111</prop>
<prop key="小马哥2">222222</prop>
<prop key="小马哥3">333333</prop>
<prop key="小马哥4">444444</prop>
<prop key="小马哥5">555555</prop>
</props>
</property>
装配一个对象dataSource对象
对象的单独定义以及使用类加载器location定义
<!--定义Peoperties对象,单独定义-->
<util:properties id="db2">
<prop key="driverClassName">oracle.jdbc.OracleDriver</prop>
<prop key="url">jdbc:oracle:thin:@localhost:1521:xe</prop>
<prop key="username">system</prop>
<prop key="password">123456</prop>
</util:properties>
<util:properties id="db" location="classpath:db.properties"></util:properties>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="#{db.driverClassName}"></property>
<property name="url">
<value>#{db.url}</value>
</property>
<property name="username" value="#{db.username}"></property>
<property name="password" value="#{db.password}"></property>
</bean>
db.properties
配置文件内容:
driverClassName=oracle.jdbc.OracleDriver
url=jdbc:oracle:thin:@localhost:1521:xe
username=system
password=123456
TestBasicDataSourceGet.java
内容:
public static void main(String[] args) throws SQLException {
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext-DI4.xml");
DataSource dataSource = app.getBean("dataSource", DataSource.class);
System.out.println(dataSource);
Connection connection = dataSource.getConnection();
System.out.println(connection);
}
Spring中的EL表达式
只要把之前的EL表达式中的$换成#就可以了
组件扫描
Spring提供了一套基于注解配置的使用方法。使用该方法可以大大简化XML配置信息。这个技术的出现可以极大的简化XML配置文件的编写。
使用方法如下:
在applicationContext.xml
中添加启用标记`<context:component-scan base-package=“包路径”>``
在组件类中追加以下标记
注解标记 | 描述 |
@Component | 通用注解 |
@Repository | 持久化层组件注解(dao) |
@Service | 业务层组件注解 |
@Controller | 控制层组件注解 |
组件扫描的实现步骤
- 建立项目,拷贝IOC和AOP的jar包到lib下
- 拷贝配置文件到src下并在配置文件中开启组件扫描
<context:component-scan base-package="包名结构" />
- 在相应的类上写下对应的标注
- 从Spring容器中获取对应的组件,默认是以类名首字母小写为标准获取组件的,当然也可以制定组件放入容器中的标识
例如,测试一下银行账户类(BankAccount)的配置及实际调用
- 建立项目,拷贝对应的jar包到lib下并配置Libraries,这里需要重要导入aop的jar包,如不导入会出现错误(什么错误?我也不知道,反正导入就不会出现错误)
- 设置配置文件xml,例如下面的配置:
<?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"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd">
<!--开启组件扫描-->
<context:component-scan base-package="com.bean"/>
</beans>
需要注意的是,在配置组件扫面的context标签中,需要设置注解标记,上面的表格都有哦,比如什么Component、Repository、Service、Controller
设置完了过后还需要配置包名,例如我们想要对bean包下的内容进行扫描注解,那么需要设置base-package="com.bean"
,到此配置文件xml就结束了
3. 我创建了一个BankAccount类,内容如下:
package com.bean;
import org.springframework.stereotype.Component;
import java.io.Serializable;
/**
* Class Describe:银行账户
*
* @author biuaxia
* @date 2018/11/7
* @time 17:10
*/
@Component
public class BankAccount implements Serializable {
private long id;
/**
* 卡号
*/
private String cardNo;
/**
* 持卡人
*/
private String accountName;
/**
* 密码
*/
private String password;
/**
* 账户余额
*/
private double money;
@Override
public String toString() {
return "BankAccount{" +
"id=" + id +
", cardNo='" + cardNo + '\'' +
", accountName='" + accountName + '\'' +
", password='" + password + '\'' +
", money=" + money +
'}';
}
public BankAccount() {
id = -1;
cardNo = "0000 0000 0000 0000";
accountName = "nnnn nnnn";
password = "123456";
money = 1.0;
}
public BankAccount(String cardNo, String accountName, String password, double money) {
this.cardNo = cardNo;
this.accountName = accountName;
this.password = password;
this.money = money;
}
public BankAccount(long id, String cardNo, String accountName, String password, double money) {
this.id = id;
this.cardNo = cardNo;
this.accountName = accountName;
this.password = password;
this.money = money;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getCardNo() {
return cardNo;
}
public void setCardNo(String cardNo) {
this.cardNo = cardNo;
}
public String getAccountName() {
return accountName;
}
public void setAccountName(String accountName) {
this.accountName = accountName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public static void main(String[] args) {
}
}
请注意,在类块儿上我们加上了注解@Component,因为在分析时,我们进行了判断,这个bean类属于持久化注解、业务层注解、控制层注解还是通用注解,经过考虑我选择了通用注解并加上了注释
从Spring容器获取中获取对应的组件
新建一个测试类,内容如下:
package com.test;
import com.bean.BankAccount;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Class Describe:
*
* @author biuaxia
* @date 2018/11/7
* @time 17:20
*/
public class ComponentScanTest {
public static void main(String[] args) {
ApplicationContext app = new ClassPathXmlApplicationContext("Component-Scan.xml");
//获取银行账户对象
BankAccount bankAccount = app.getBean("bankAccount", BankAccount.class);
System.out.println(bankAccount);
}
}
打印结果
至此组件扫描从导入jar包 -> 创建bean对应的配置文件 -> 创建对应的bean类 -> 测试从Spring容器获取对应的组件 就完成了
集合的单独定义及引入
单独定义的用处在于有些数据需要多次调用的话,就不需要每次都在<property>
中单独定义在调用了,增加复用性
props
,list
,set
,map
都可以被单独的抽取出去
如果要定义list
,我们需要在<bean/>
标签外定义,并且使用<util:list/>
标签定义
<list/>
-><util:list/>
<set/>
-><util:set/>
<map/>
-><util:map/>
<properties/>
-><util:properties/>
记得给单独定义的集合加上id,在属性标签<property>
使用ref
属性来引用这个集合
在定义扫描对象时有三种方式
@Component
@Component("val")
@Component(value="val")
这三种都可以哟
自定义测试组件扫描
文件结构如下
bean包:
com.bean.BankAccount
com.bean.MsgBean
dao包com.dao.BankAccountDAO
com.dao.BankAccountDAOImp
test包com.test.BeanAndDaoScanTest
BankAccount
仅在原有基础上加上了标注@Component
MsgBean
也一样只加了标注@Component
3.配置文件BeanAndDaoScan.xml
内容:
<?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"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd">
<!--开启组件扫描-->
<context:component-scan base-package="com.bean,com.dao"/>
</beans>
- 测试类
BeanAndDaoTest
package com.test;
import com.bean.BankAccount;
import com.bean.MsgBean;
import com.dao.BankAccountDaoImp;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Class Describe:测试配置文件是否成功扫描
*
* @author biuaxia
* @date 2018/11/8
* @time 8:59
*/
public class BeanAndDaoScanTest {
public static void main(String[] args) {
ApplicationContext app = new ClassPathXmlApplicationContext("BeanAndDaoScan.xml");
System.out.println("测试获取MsgBean对象:\t\t\t" + app.getBean("msgBean", MsgBean.class));
System.out.println("测试获取BankAccount对象:\t\t" + app.getBean("bankAccount", BankAccount.class));
System.out.println("测试获取BankAccountDaoImp对象:\t\t" + app.getBean("bankAccountDaoImp", BankAccountDaoImp.class));
System.out.println("测试获取BankAccountDao对象:\t\t" + app.getBean("bankAccountDao", BankAccountDaoImp.class));
}
}
结论
在测试类中,对bean包下的两个对象获取没有任何问题,其中获取时,我没有设置类的id,而是默认的配置@Component
,这个时候获取就要使用类名首字母小写的方式获取Bean,在获取dao时出现问题,不能够获取到dao,但是可以获取实现类的对象,所以这里不能这样获取dao接口
自己写一个BankAccountDAO接口
定义一个根据卡号获取账户信息的方法,然后写一个实现类对这个方法做一个伪实现,最后把实现类对象以标注的形式加载到Spring容器中,最后通过Spring容器获取实现类对象并根据卡号获取对象的方法
文件结构如下:
bean包:
com.bean.BankAccount
dao包
com.dao.BankAccountDAO
com.dao.BankAccountDaoImp
test包
com.test.BeanAndDaoScanTest
- com.bean.BankAccount
package com.bean;
import org.springframework.stereotype.Component;
import java.io.Serializable;
/**
* Class Describe:银行账户
*
* @author biuaxia
* @date 2018/11/7
* @time 17:10
*/
@Component
public class BankAccount implements Serializable {
private long id;
/**
* 卡号
*/
private String cardNo;
/**
* 持卡人
*/
private String accountName;
/**
* 密码
*/
private String password;
/**
* 账户余额
*/
private double money;
@Override
public String toString() {
return "BankAccount{" +
"id=" + id +
", cardNo='" + cardNo + '\'' +
", accountName='" + accountName + '\'' +
", password='" + password + '\'' +
", money=" + money +
'}';
}
public BankAccount() {
id = -1;
cardNo = "0000 0000 0000 0000";
accountName = "nnnn nnnn";
password = "123456";
money = 1.0;
}
public BankAccount(String cardNo, String accountName, String password, double money) {
this.cardNo = cardNo;
this.accountName = accountName;
this.password = password;
this.money = money;
}
public BankAccount(long id, String cardNo, String accountName, String password, double money) {
this.id = id;
this.cardNo = cardNo;
this.accountName = accountName;
this.password = password;
this.money = money;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getCardNo() {
return cardNo;
}
public void setCardNo(String cardNo) {
this.cardNo = cardNo;
}
public String getAccountName() {
return accountName;
}
public void setAccountName(String accountName) {
this.accountName = accountName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public static void main(String[] args) {
}
}
- com.dao.BankAccountDAO
package com.dao;
import com.bean.BankAccount;
/**
* @author: gsdad
*/
public interface BankAccountDAO {
/**
* 获取账户信息的方法
*
* @param cardNo
* @return
*/
BankAccount getAccountByCardNo(String cardNo);
}
- com.dao.BankAccountDaoImp
package com.dao;
import com.bean.BankAccount;
import org.springframework.stereotype.Repository;
/**
* Class Describe:
*
* @author biuaxia
* @date 2018/11/7
* @time 21:47
*/
@Repository("baDao")
public class BankAccountDaoImp implements BankAccountDAO {
/**
* 实现获取账户信息的方法
*/
@Override
public BankAccount getAccountByCardNo(String cardNo) {
System.out.println("获取数据ing...");
return null;
}
}
- com.service.BankAccountService
package com.service;
import com.bean.BankAccount;
import com.dao.BankAccountDAO;
import org.springframework.stereotype.Service;
/**
* Class Describe:
*
* @author biuaxia
* @date 2018/11/7
* @time 21:50
*/
@Service("baSer")
public class BankAccountService {
private BankAccountDAO dao;
// private BankAccountDAO dao = new BankAccountDaoImp();
/**
* 获取用户信息的service
*
* @return
*/
public BankAccount getBankAccount(String cardNo) {
return dao.getAccountByCardNo(cardNo);
}
}
- com.test.BankAccountServiceTest
package com.test;
import com.service.BankAccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Class Describe:测试能否拿到BankAccountService对象并调用实现方法
*
* @author biuaxia
* @date 2018/11/8
* @time 10:19
*/
public class BankAccountServiceTest {
public static void main(String[] args) {
ApplicationContext app = new ClassPathXmlApplicationContext("BeanAndDaoScan.xml");
BankAccountService service = app.getBean("baSer", BankAccountService.class);
System.out.println(service);
service.getBankAccount("1234");
}
}
- BankAccountTest.xml
<?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"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd">
<!--开启组件扫描-->
<context:component-scan base-package="com.bean,com.dao,com.service"/>
</beans>
执行测试类的main方法,会出现空指针异常
Exception in thread "main" java.lang.NullPointerException
由此引入组件装配的标注内容
和组件装配有关的标注
@Autowried
/@Qualifier
@Resource
其中,1可以处理构造器注入和Setter注入,2只能处理Setter注入,但大部分情况都是Setter注入
1还可以加在字段或者setter方法或者构造函数上面
首先根据类型加在对应的组件,类型找不到则根据名字查找,这个标注也可以指定名字进行查找,如果对应的名字都找不到就直接停止并且报错鸭
指定名字查找使用@Qualifier
,自动匹配使用@Autowried
@Autowried(required=true)
这是默认配合,则必须找到对应的组件,没有程序直接崩溃,也可以指定为false,找不到对应的组件就忽略了鸭
2@Resource
可以加在字段或者setter方法,但这通常就够用了,这个标注他不是Spring框架的,它是Java中的标准标注,但可以和Spring无缝结合
@Resource
优先按照名字查找,如果找不到则按照类型查找
当然也可以直接指定名字进行查找,注意了,指定名字必须使用@Resource(name = "baDao")
这样的格式
其他标注
@PostConstructor
初始化方法的标注@PreDestroy
对象销毁之前调用的标注@Value
可以使用Spring EL表达式注入值
例子:
扫描器配置好扫描路径就ok,不多写下来了~
Emp
bean类
package com.bean;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
/**
* Class Describe:员工类
*
* @author biuaxia
* @date 2018/11/8
* @time 11:40
*/
@Component
public class Emp {
private String name;
public Emp() {
System.out.println("无参构造");
}
@PostConstruct
public void init() {
System.out.println("初始化?");
}
@PreDestroy
public void destory() {
System.out.println("我被销毁了?");
}
}
TestEmp
测试类
package com.test;
import com.bean.Emp;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Class Describe:
*
* @author biuaxia
* @date 2018/11/8
* @time 11:43
*/
public class TestEmp {
public static void main(String[] args) {
// ApplicationContext app = new ClassPathXmlApplicationContext("BeanAndDaoScan.xml");
AbstractApplicationContext app = new ClassPathXmlApplicationContext("BeanAndDaoScan.xml");
Emp emp = app.getBean("emp", Emp.class);
System.out.println(emp);
app.close();
}
}
@Value
的例子
配置文件BankAndDaoScab.xml
<?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"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd">
<!--开启组件扫描-->
<context:component-scan base-package="com.bean,com.dao,com.service"/>
<util:properties id="db2">
<prop key="driverClassName">oracle.jdbc.OracleDriver</prop>
<prop key="url">jdbc:oracle:thin:@localhost:1521:xe</prop>
<prop key="username">system</prop>
<prop key="password">123456</prop>
</util:properties>
</beans>
Emp
bean类
package com.bean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
/**
* Class Describe:员工类
*
* @author biuaxia
* @date 2018/11/8
* @time 11:40
*/
@Component
public class Emp {
@Value("#{db2.username}")
private String name;
@Value("#{db2.password}")
private String pws;
@Value("#{db2.url}")
private String url;
@Override
public String toString() {
return "Emp{" +
"name='" + name + '\'' +
", pws='" + pws + '\'' +
", url='" + url + '\'' +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPws() {
return pws;
}
public void setPws(String pws) {
this.pws = pws;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public Emp() {
System.out.println("无参构造");
}
@PostConstruct
public void init() {
System.out.println("初始化?");
}
@PreDestroy
public void destory() {
System.out.println("我被销毁了?");
}
}
测试类TestEmp
package com.test;
import com.bean.Emp;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Class Describe:
*
* @author biuaxia
* @date 2018/11/8
* @time 11:43
*/
public class TestEmp {
public static void main(String[] args) {
// ApplicationContext app = new ClassPathXmlApplicationContext("BeanAndDaoScan.xml");
AbstractApplicationContext app = new ClassPathXmlApplicationContext("BeanAndDaoScan.xml");
Emp emp = app.getBean("emp", Emp.class);
System.out.println(emp);
app.close();
}
}
第二天的所有内容到此结束