jee第2章 ---spring的Bean(代码)

mac2024-11-12  12

一、Bean的实例化

构造器实例化静态工厂实例化实例工厂方式实例化

1. 构造器实例化 (1)Bean1.java

package com.itheima.instance.constructor; public class Bean1 { }

(2)bean1.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <bean id="bean1" class="com.itheima.instance.constructor.Bean1" /> </beans>

(3)

package com.itheima.instance.constructor; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class InstanceTest1 { public static void main(String[] args) { String xmlPath = "com/itheima/instance/constructor/bean1.xml"; ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath); Bean1 bean = (Bean1) applicationContext.getBean("bean1"); System.out.println(bean); } }

结果展示: 2. 静态工厂实例化 (1)Bean2.java

package com.itheima.instance.static_factory; public class Bean2 { }

(2)MyBean2Factory.java

package com.itheima.instance.static_factory; public class MyBean2Factory { public static Bean2 createBean() { return new Bean2(); } }

(3)

<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <bean id="bean2" class="com.itheima.instance.static_factory.MyBean2Factory" factory-method="createBean" /> </beans>

(4)测试类 InstanceTest2.java

package com.itheima.instance.static_factory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class InstanceTest2 { public static void main(String[] args) { String xmlPath="com/itheima/instance/static_factory/bean2.xml"; ApplicationContext applicationContext= new ClassPathXmlApplicationContext(xmlPath); System.out.print(applicationContext.getBean("bean2")); } }

输出结果展示: 3.实例工厂方式实例化 (1)Bean3.java

package com.itheima.instance.factory; public class Bean3 { }

(2)MyBean3Factory.java

package com.itheima.instance.factory; public class MyBean3Factory { public MyBean3Factory(){ System.out.println("Bean3工厂实例化"); } public Bean3 createBean() { return new Bean3(); } }

(3)bean3.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <bean id="MyBean3Factory" class="com.itheima.instance.factory.MyBean3Factory" /> <bean id="bean3" factory-bean="MyBean3Factory" factory-method="createBean" /> </beans>

(4)InstanceTest3.java

package com.itheima.instance.factory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class InstanceTest3 { public static void main(String[] args) { String xmlPath="com/itheima/instance/factory/bean3.xml"; ApplicationContext applicationContext= new ClassPathXmlApplicationContext(xmlPath); System.out.print(applicationContext.getBean("bean3")); } }

输出结果展示: 二、Bean的作用域

singleton作用域prototype作用域 1.singleton(单例) (1)Scope.java package com.itheima.scope; public class Scope { }

(2)beans4.java

<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <bean id="scope" class="com.itheima.scope.Scope" /> </beans>

ScopeTest

package com.itheima.scope; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class ScopeTest { public static void main(String[] args) { String XmlPath="com/itheima/scope/beans4.xml"; ApplicationContext applicationContext = new ClassPathXmlApplicationContext(XmlPath); System.out.println(applicationContext.getBean("scope")); System.out.println(applicationContext.getBean("scope")); } }

输出:

prototype(原型)

将1中的(2)beans的代码,把scope的默认改为prototype原型,其他都不变,再运行代码 (2)beans4.java

<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <bean id="scope" class="com.itheima.scope.Scope" scope="prototype" /> </beans>

输出:

三、Bean的装配方式 Bean的装配注入即Bean的以来注入方式,有以下几种方式:

基于XML的装配 设值注入参数构造注入 基于注解的装配自动装配

1.基于XML的装配 (1)User.java

package com.itheima.assemble; import java.util.List; public class User { private String username; private Integer password; private List<String> list; /** * 1. 使用构造注入 * 1.1 提供带所有参数的有参构造方法 */ public User(String username, Integer password, List<String> list) { super(); this.username = username; this.password = password; this.list = list; } /** * 2. 使用设值注入 * 2.1 提供默认空参构造方法 * 2.2 为所有属性提供setter方法 */ public User() { super(); } public void setUsername(String username) { this.username = username; } public void setPassword(Integer password) { this.password = password; } public void setList(List<String> list) { this.list = list; } @Override public String toString() { return "User [username =" + username + ",password = " + password + ", list = "+list + "]"; } }

(2) beans5.java

<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <!-- 构造注入 --> <bean id="User1" class="com.itheima.assemble.User"> <constructor-arg index="0" value="tom" /> <constructor-arg index="1" value="123456" /> <constructor-arg index="2"> <list> <value>"constructorvalue1"</value> <value>"constructorvalue2"</value> </list> </constructor-arg> </bean> <!-- 设值注入--> <bean id="User2" class="com.itheima.assemble.User"> <property name="username" value="zhangsan"></property> <property name="password" value="654321"></property> <property name="list"> <list> <value>"setlistvalue1"</value> <value>"setlistvalue2"</value> </list> </property> </bean> </beans>

(3) XmlBeanAssembleTest.java

package com.itheima.assemble; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class XmlBeanAssembleTest { public static void main(String[] args) { String xmlPath="com/itheima/assemble/beans5.xml"; ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath); System.out.println(applicationContext.getBean("User1")); System.out.println(applicationContext.getBean("User2")); } }

输出: 2.基于注解的装配 @Repository(“DAO层的Bean实例名”) @Service(“Service层的Bean实例名”) @Controller(“Controller控制层的Bean实例名”)

// <bean id ="xYY" class=“..." >

@Resource(name=“DAO层或Service层的Bean实例名”)

//相当于嵌套在bean标签内的<property name="xY" ref="xY"/>

(1)com.javaee.ch02.annotation.dao包 @Repository:将数据访问层(DAO)的类标识为Spring中的Bean

UserDao.java package com.javaee.ch02.annotation.dao; public interface UserDao { public void save(); } UserDaoImpl.java package com.javaee.ch02.annotation.dao; import org.springframework.stereotype.Repository; @Repository("userDao") //@Repository 将UserDaoImpl类标识为Spring类的Bean //等价于<bean id="userDao" class="com.javaee.ch02.annotation.dao.UserDaoImpl" /> public class UserDaoImpl implements UserDao { @Override public void save() { System.out.println("userDao:save!"); } }

(2)com.javaee.ch02.annotation.service @Service:将业务层(Service)的类标识为Spring中的Bean @Resource:其作用与Autowired一样(对Bean的属性变量、属性的setter方法及构造方法进行标注,配合对应的注解处理器完成Bean的自动配置工作)。@Resource中有两个重要属性:name和type,被Spring分别解析为Bean实例的名称和类型

UserService.java package com.javaee.ch02.annotation.service; public interface UserService { public void save(); } UserServiceImpl.java package com.javaee.ch02.annotation.service; import javax.annotation.Resource; import org.springframework.stereotype.Service; import com.javaee.ch02.annotation.dao.UserDao; import com.javaee.ch02.annotation.service.UserService; @Service("userService") //@Service将UserServiceImpl注解为Spring标识的Bean //等价于<bean id="userService" class="com.javaee.ch02.annotation.service.UserServiceImpl" /> public class UserServiceImpl implements UserService { @Resource(name="userDao") //@Resource注解标注在属性userDao //等价于<property name="userDao" ref="userDao" /> private UserDao userDao; /* public void setUserDao(UserDao userDao) { this.userDao = userDao; }*/ @Override public void save() { userDao.save(); System.out.println("userService:save!"); } }

(3)com.javaee.ch02.annotation.controller @Controller:将控制层(Controller)的类标识为Spring中的Bean

UserController.java package com.javaee.ch02.annotation.controller; public interface UserController { public void save(); } UserControllerImpl.java package com.javaee.ch02.annotation.controller; import javax.annotation.Resource; import org.springframework.stereotype.Controller; import com.javaee.ch02.annotation.service.UserService; @Controller("userController") //@Service注解标注了UserController //等价于配置文件中 <bean id="userService" class="UserControllerImpl" /> public class UserControllerImpl implements UserController { @Resource(name="userService") //@Resource注解标注在属性userService //等价于<property name="userService" ref="userService" /> private UserService userService; /* public void setUserService(UserService userService) { this.userService = userService; }*/ @Override public void save() { userService.save(); System.out.println("userController:save!"); } }

(4) applicationContext.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 使用context 命名空间,在配置文件中开启相应的注解处理器 --> <context:annotation-config/> <!-- 分别定义3个Bean实例 --> <bean id="userDao" class="com.javaee.ch02.annotation.dao.UserDaoImpl"/> <bean id="userService" class="com.javaee.ch02.annotation.service.UserServiceImpl"/> <bean id="userController" class="com.javaee.ch02.annotation.controller.UserControllerImpl"/> </beans>

(5) TestAnnotation.java

package com.javaee.ch02.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.javaee.ch02.annotation.controller.UserController; public class TestAnnotation { @SuppressWarnings("resource") public static void main(String[] args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContextAnnotation.xml"); UserController userController = (UserController) applicationContext.getBean("userController"); userController.save(); System.out.println("--------对包路径下的所有Bean扫描-----------"); // <context:component-scan base-package="com.javaee.ch02.annotation"/>用此句后,不再逐个写该包路径(含子包)下的各个bean了。 applicationContext = new ClassPathXmlApplicationContext("applicationContextAnnotation2.xml"); UserController userController2 = (UserController) applicationContext.getBean("userController"); userController2.save(); } } 关于注解的装配 与XML装备比所不同的是, 这里不需要配置子元素<property> 在配置文件中,需要使用 <context:annotation-config/>开启相应的注解处理器;另配置文件中的约束信息与之前的有很大不同<context:component-scan base-package=“com.javaee.ch02.annotation”/>它是通过扫描包路径下的所有bean文件(即:用此句后,不需要再逐个写该包路径(含子包)下的各个bean了。)

3.自动装配 修改注解的代码即可,为方便,重新建包名为com.javaee.ch02.autowire.*; (1)修改UserDaoImpl.java

package com.javaee.ch02.autowire.dao; public class UserDaoImpl implements UserDao { @Override public void save() { System.out.println("userDao:save!"); } }

(2)修改UserServiceImpl.java

package com.javaee.ch02.autowire.service; import com.javaee.ch02.autowire.dao.UserDao; import com.javaee.ch02.autowire.service.UserService; public class UserServiceImpl implements UserService { private UserDao userDao; public void setUserDao(UserDao userDao) { this.userDao = userDao; } @Override public void save() { userDao.save(); System.out.println("userService:save!"); } }

(3)修改UserControllerImpl.java

package com.javaee.ch02.autowire.controller; import com.javaee.ch02.autowire.service.UserService; public class UserControllerImpl implements UserController { private UserService userService; //下面三行不可以省略 public void setUserService(UserService userService) { this.userService = userService; } @Override public void save() { userService.save(); System.out.println("userController:save!"); } }

(4)applicationContextAutoWire.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"> <!-- --> <!-- 分别定义3个Bean实例 --> <!-- <bean id="userDao" class="com.javaee.ch02.autowire.dao.UserDaoImpl"/> <bean id="userService" class="com.javaee.ch02.autowire.service.UserServiceImpl"> <property name="userDao" ref="userDao"/> </bean> <bean id="userController" class="com.javaee.ch02.autowire.controller.UserControllerImpl"> <property name="userService" ref="userService"/> </bean> --> <bean id="userDao" class="com.javaee.ch02.autowire.dao.UserDaoImpl"/> <bean id="userService" class="com.javaee.ch02.autowire.service.UserServiceImpl" autowire="byName"/> <bean id="userController" class="com.javaee.ch02.autowire.controller.UserControllerImpl" autowire="byName"/> </beans> <!-- 如不想在bean标签的内部 逐条写property name="" ref="" 之类的,就可以用autowire="byName"即可 -->

(5)测试类

package com.javaee.ch02.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.javaee.ch02.autowire.controller.UserController; public class TestAutoWire { // 测试在配置bean标签时,用autowire="byName"来省略该Bean含的property子标签 public static void main(String[] args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContextAutoWire.xml"); UserController userController = (UserController) applicationContext.getBean("userController"); userController.save(); } }

最后两大点的代码学习自老师的代码,开头开始的代码是学习自课本

最新回复(0)