一、基本使用

1、Bean的基本配置

  • id属性:定义bean的名字
  • class属性:定义bean的类名称
  • name属性:bean的别名配置,可以有逗号,分号,空格分隔
  • scope属性:控制对象是否为单例
  • autowire:自动装配

2、实例化Bean的三种方式

  • 构造方法(常用)
  • 静态工厂(了解)
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import dao.UserDao;

import service.impl.UserServiceImpl;

import bean.User;


@SuppressWarnings("unused")
public class Test3 {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext3.xml");
UserDao userDao = (UserDao) context.getBean("userDao");
userDao.save();
}
}
<?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 id="userDao" class="factory.UserDaoFactory" factory-method="getUserDao"></bean>
</beans>
package factory;

import dao.UserDao;
import dao.impl.UserDaoImpl;

public class UserDaoFactory {

public static UserDao getUserDao()
{
return new UserDaoImpl();
}
}
  • 实例工厂
  • 使用factoryBean(实用)

UserDaoFactoryBean.java

package factory;

import org.springframework.beans.factory.FactoryBean;

import dao.UserDao;
import dao.impl.UserDaoImpl;

public class UserDaoFactoryBean implements FactoryBean<UserDao> {

//创建对象的方法
public UserDao getObject() throws Exception {

return new UserDaoImpl();
}

public Class<?> getObjectType() {

return UserDao.class;
}

public boolean isSingleton() {
// TODO Auto-generated method stub
return false;
}
}
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import dao.UserDao;

import service.impl.UserServiceImpl;

import bean.User;


@SuppressWarnings("unused")
public class Test3 {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext3.xml");
UserDao userDao = (UserDao) context.getBean("userDao");
userDao.save();
}
}

applicationContext3.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="userDao" class="factory.UserDaoFactoryBean"></bean>
</beans>

3、bean的生命周期

<?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 id="userDao" class="dao.impl.UserDaoImpl" init-method="init" destroy-method="destroy"></bean>
</beans>
package dao.impl;

import dao.UserDao;


public class UserDaoImpl implements UserDao {

public void init() {
System.out.println("init...");
}
public void save() {
System.out.println("UserDaoImpl save...");
}

public void destroy() {
System.out.println("destroy...");
}
}
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import dao.UserDao;

import service.impl.UserServiceImpl;

import bean.User;


@SuppressWarnings("unused")
public class Test3 {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext3.xml");
UserDao userDao = (UserDao) context.getBean("userDao");
userDao.save();
context.registerShutdownHook();
}
}

spring的方式 - InitializingBean,DisposableBean接口

package service.impl;

import javax.annotation.Resource;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;

import service.UserService;
import dao.UserDao;

@SuppressWarnings("restriction")
@Service("userService")
public class UserServiceImpl implements UserService,InitializingBean,DisposableBean {

@Resource(name = "userDao")
private UserDao userDao;

public void save() {
userDao.save();
System.out.println("UserServiceImpl save...");
}

public void afterPropertiesSet() throws Exception {
System.out.println("afterPropertiesSet...");
}

public void destroy() throws Exception {
System.out.println("destroy...");
}

// public void setUserDao(UserDao userDao) {
// this.userDao = userDao;
// }

}

4、依赖注入方式4种

a、setter注入

Java 之Spring框架Bean_xml

 Java 之Spring框架Bean_spring_02

 b、构造器注入

 UserDaoImpl.java

package service.impl;

import service.UserService;
import dao.UserDao;

public class UserServiceImpl implements UserService {

private UserDao userDao;

//创建UserServiceImpl 的构造方法
public UserServiceImpl(UserDao userDao1) {
this.userDao = userDao1;
}

public void save() {
userDao.save();
System.out.println("UserServiceImpl save...");
}

}

applicationContext4.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="userDao" class="dao.impl.UserDaoImpl"></bean>

<bean id="userService" class="service.impl.UserServiceImpl">
<!-- index:构造方法中的参数位置,ref:关联上面bean中的id -->
<constructor-arg index="0" ref="userDao"></constructor-arg>
</bean>
</beans>

运行代码

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import service.UserService;


public class Test4 {

public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext4.xml");
UserService userService = (UserService) context.getBean("userService");
userService.save();
}

}

c、自动装配(autowire)

UserServiceImpl.java

package service.impl;

import service.UserService;
import dao.UserDao;

public class UserServiceImpl implements UserService {

private UserDao userDao;

public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}

public void save() {
userDao.save();
System.out.println("UserServiceImpl save...");
}

}

运行代码

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import service.UserService;


public class Test5 {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext5.xml");
UserService userService = (UserService) context.getBean("userService");
userService.save();
}
}

applicationContext5.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean class="dao.impl.UserDaoImpl"></bean>
<!--autowire属性:开启自动装配,通常使用按类型装配-->
<bean id="userService" class="service.impl.UserServiceImpl" autowire="byType"></bean>
</beans>

 d、集合注入

User.java

Java 之Spring框架Bean_java_03Java 之Spring框架Bean_spring_04

package bean;

import java.util.Map;
import java.util.Set;

public class User {
private int id;
private String name;
private Set<String> set;
private Map<String, String> map;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Set<String> getSet() {
return set;
}

public void setSet(Set<String> set) {
this.set = set;
}

public Map<String, String> getMap() {
return map;
}

public void setMap(Map<String, String> map) {
this.map = map;
}

@Override
public String toString() {
return "User [id=" + id + ", map=" + map + ", name=" + name + ", set="
+ set + "]";
}

}

View Code

运行代码

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import bean.User;


public class Test6 {

public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext6.xml");
User user = (User) context.getBean("user");
System.out.println(user);
}

}

applicationContext6.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="user" class="bean.User">
<property name="id" value="1"></property>
<property name="name" value="yy"></property>
<property name="set">
<set>
<value>11</value>
<value>22</value>
<value>33</value>
</set>
</property>
<property name="map">
<map>
<entry key="key1" value="11"></entry>
<entry key="key2" value="22"></entry>
</map>
</property>
</bean>

</beans>

Java 之Spring框架Bean_xml_05