bean

beans有无状态

帅比萌擦擦* 提交于 2020-02-27 05:43:17
Spring Bean Scopes https://www.tutorialspoint.com/spring/spring_bean_scopes.htm When defining a <bean> you have the option of declaring a scope for that bean. For example, to force Spring to produce a new bean instance each time one is needed, you should declare the bean's scope attribute to be prototype . Similarly, if you want Spring to return the same bean instance each time one is needed, you should declare the bean's scope attribute to be singleton . The Spring Framework supports the following five scopes, three of which are available only if you use a web-aware ApplicationContext. Sr.No.

spring的InitializingBean的 afterPropertiesSet 方法 和 init-method配置的区别联系

六眼飞鱼酱① 提交于 2020-02-27 05:42:40
InitializingBean Spirng的InitializingBean为bean提供了定义初始化方法的方式。InitializingBean是一个接口,它仅仅包含一个方法:afterPropertiesSet()。 Bean实现这个接口,在afterPropertiesSet()中编写初始化代码: package research.spring.beanfactory.ch4; import org.springframework.beans.factory.InitializingBean; class LifeCycleBean implements InitializingBean{ void afterPropertiesSet() throws Exception { System. out .println("LifeCycleBean initializing..."); } } 在xml配置文件中并不需要对bean进行特殊的配置: xml version="1.0" encoding="UTF-8" ?> DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" > < beans > < bean name =

action和actionListener之间的区别

我的未来我决定 提交于 2020-02-27 01:43:48
action 和 actionListener 什么区别,什么时候应该使用 action 和 actionListener ? #1楼 在调用Action并确定下一页的位置之前,将首先触发ActionListener并提供修改响应的选项。 如果您在同一页面上有多个按钮,这些按钮应该移至相同的位置,但执行的操作略有不同,则可以为每个按钮使用相同的Action,但使用不同的ActionListener处理稍微不同的功能。 这是描述关系的链接: http://www.java-samples.com/showtutorial.php?tutorialid=605 #2楼 actionListener 如果您想 在 执行实际业务操作 之前 有一个钩子 , 请使用 actionListener ,例如将其记录和/或设置其他属性(通过 <f:setPropertyActionListener> ),和/或访问调用了动作(可通过 ActionEvent 参数获得)。 因此,纯粹是为了在调用实际业务操作之前进行准备。 默认情况下, actionListener 方法具有以下签名: import javax.faces.event.ActionEvent; // ... public void actionListener(ActionEvent event) { // ... }

Java类型转换报错java.lang.ClassCastException: com.sun.proxy.$Proxy7 cannot be cast to

岁酱吖の 提交于 2020-02-27 01:40:17
类型转换异常 java.lang.ClassCastException: com.sun.proxy.$Proxy7 cannot be cast to 这个错误的原因只有一个类型转换异常。 在一个子类实现父类的接口时,并且在强制转换时一定要使用父类的接口,否则就会报这个错误。 接口代码 package com.xxr.mapper; public interface UserMapper { void AddUser(); void SelectUser(); void DeleteUser(); void UpUser(); } 子类代码 package com.xxr.mapper.Impl; import com.xxr.mapper.UserMapper; public class UserImpl implements UserMapper { public void AddUser() { System.out.println("增加"); } public void SelectUser() { System.out.println("查询"); } public void DeleteUser() { System.out.println("删除"); } public void UpUser() { System.out.println("修改"); } }

Spring 循环依赖如何解决

不羁的心 提交于 2020-02-26 23:15:26
循环依赖是什么? Bean A 依赖 B,Bean B 依赖 A这种情况下出现循环依赖。 Bean A → Bean B → Bean A 更复杂的间接依赖造成的循环依赖如下。 Bean A → Bean B → Bean C → Bean D → Bean E → Bean A 循环依赖会产生什么结果? 当Spring正在加载所有Bean时,Spring尝试以能正常创建Bean的顺序去创建Bean。 例如,有如下依赖: Bean A → Bean B → Bean C Spring先创建beanC,接着创建bean B(将C注入B中),最后创建bean A(将B注入A中)。 但当存在循环依赖时,Spring将无法决定先创建哪个bean。这种情况下,Spring将产生异常BeanCurrentlyInCreationException。 3.普通注入之间的循环依赖 比如:我现在有一个ServiceA需要调用ServiceB的方法,那么ServiceA就依赖于ServiceB,那在ServiceB中再调用ServiceA的方法,就形成了循环依赖。Spring在初始化bean的时候就不知道先初始化哪个,bean就会报错。 public class ClassA { @Autowired ClassB classB; } public class ClassB { @Autowired

spring总结

旧巷老猫 提交于 2020-02-26 22:57:40
spring的三大特性:IOC(控制反转)和DI(依赖注入)以及AOP(面向切面编程) IOC(控制反转): 控制反转,即将创建对象的工作由自己交给了spring容器来完成,由spring容器来维护依赖关系,来掌控对象的生命周期。 DI(依赖注入) 依赖注入:将值通过配置的方式为变量初始化。DI带来了松耦合,没有DI,对象需要和其依赖的对象一起初始化,这样造成了紧密耦合。 AOP(面向切面编程) 面向切面编程就是在运行时,动态地将通知(增强的代码)织入到类的指定方法、指定位置上的编程方式 依赖注入的三种方式: (1)构造方法注入 (2)setter注入 (3)基于注解的注入 spring容器: 对象生存于spring容器中,spring容器负责创建对象,配置并管理它们的整个生命周期,最后销毁它们。 spring容器分为bean工厂和应用上下文:常用应用上下文。 装配 :创建应用对象的协同关系 装配的3种方式: 在XML中进行显式配置 在JAVA中进行显式配置 隐式的bean发现机制和自动装配 @Component 在一个类上使用@Component,表示将为这个类会作为组件类,并且告诉spring要为这个类创建一个bean @Autowired 自动装配,是让spring自动满足bean依赖的一种方法,spring会自动从应用上下文中寻找所需的bean。 @Bean

SSM整合

不羁的心 提交于 2020-02-26 22:23:22
目录 一. 项目准备 01-需求 02-数据库脚本 03-项目结构 04-Maven依赖 二.Java源代码 01-domain-实体类 02-dao-持久层 03-Service-服务层 04-controller-控制器 三.Resources配置资源 01-MySQL连接信息配置 02-日志记录配置 03-SpringMVC配置 04-SSM整合配置 05-Mybatis操作Sql配置 四.Web 01-目录结构 02-服务器启动后相关配置 03-首页操作JSP页面 04-展示账户列表页面 五.SSM基本架构代码下载 一. 项目准备 01-需求 02-数据库脚本 create database ssm; create table account( id int primary key auto_increment, name varchar(100), money double(7,2), ); INSERT INTO account(NAME,money)VALUES("小雪",1000); INSERT INTO account(NAME,money)VALUES("丽丽",1000); 03-项目结构 04-Maven依赖 pom.xml文件中添加依赖 <properties> <project.build.sourceEncoding>UTF-8</project

Spring有哪些配置方式

有些话、适合烂在心里 提交于 2020-02-26 18:16:57
1、XML 配置文件。 Bean 所需的依赖项和服务在 XML 格式的配置文件中指定。这些配置文件通常包含许多 bean 定义和特定于应用程序的配置选项。它们通常以 bean 标签开头。例如: <bean id="studentBean" class="org.edureka.firstSpring.StudentBean"> <property name="name" value="Edureka"></property> </bean>    2、注解配置。 您可以通过在相关的类,方法或字段声明上使用注解,将 Bean 配置为组件类本身,而不是使用 XML 来描述 Bean 装配。默认情况下,Spring 容器中未打开注解装配。因此,您需要在使用它之前在 Spring 配置文件中启用它。例如: <beans> <context:annotation-config/> <!-- bean definitions go here --> </beans>    @Service @Component @Repository @Controlle    3、Java Config 配置。 Spring 的 Java 配置是通过使用 @Bean 和 @Configuration 来实现。 @Bean 注解扮演与 <bean /> 元素相同的角色。用到方法上

【spring学习笔记 七】ConfigurationClassPostProcessor详解

删除回忆录丶 提交于 2020-02-26 18:09:25
文章目录 概览 ConfigurationClassPostProcessor postProcessBeanDefinitionRegistry() parse() @Component @PropertySource @ ComponentScan,@ComponentScans @Import 实现ImportSelector 实现ImportBeanDefinitionRegistrar接口 @Bean load() postProcessBeanFactory() enhanceConfigurationClasses() BeanMethodInterceptor 触发条件 作用 BeanFactoryAwareMethodInterceptor 触发条件 作用 总结 概览 ConfigurationClassPostProcessor.class 主要解决的问题就是去处理spring定义的一些配置注解,例如 @Configuration , @Import , @ComponentScan , @Bean 等。 ConfigurationClassPostProcessor 实现了 BeanDefinitionRegistryPostProcessor ( BeanFactoryPostProcessor 的子类)。 PriorityOrdered ,该类优先执行。

Spring框架(二):Spring整合Mybatis、添加事物

﹥>﹥吖頭↗ 提交于 2020-02-26 14:56:41
Spring 整合 MyBatis 案例 实现功能:在数据库中根据id查找用户的信息 一、创建与数据库表中对应的实体类 使用 lombok 简化创建 User 类 package com . kuang . pojo ; @Data @AllArgsConstructor @NoArgsConstructor public class User { //注意 字段与表中列名对应 private int id ; private String name ; private String pwd ; } 二、写出这个类的映射接口,接口里有我们查询数据库的方法 package com . kuang . mapper ; import com . kuang . pojo . User ; import java . util . List ; public interface UserMapper { public List < User > getUserList ( ) ; } 三、写出接口的XML文件 xml 中有查询语句 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd