Spring

1.1.1.1 创建一个bean

package com.zt.spring;

public class MyBean {

    private String userName;

    private Integer userAge;

}

  

1.1.1.2 配置Config 配置bean

package com.zt.spring;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; @Configuration
public class MyBeanConfig {
// 和xml中配置文件的bean的标签是一样的
@Bean(name = "beanName")
public MyBean createBean(){
return new MyBean();
}
}

  

1.1.1.3 配置bean 单例还是多例的

package com.zt.spring;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope; @Configuration
public class MyBeanConfig { @Bean(name = "beanName")
// 默认值是多例的 xml的方式可以在bean的标签上面 设置这个参数
@Scope("prototype")
public MyBean createBean(){
return new MyBean();
} }

  

1.1.1.4 获取上文,获取bean

package com.zt.spring;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class App {
public static void main(String[] args) {
// 获取上下文
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyBeanConfig.class);
// 通过bean的那么获取bean
System.out.println(context.getBean("beanName"));
// 获取bean的class文件获取
System.out.println(context.getBean(MyBean.class));
context.close();
}
}

  

1.1.2 通过FactoryBean实现bena的装配

1.1.2.1 创建一个bean

package com.zt.spring;

public class Car {
}

  

1.1.2.2 配置Config 配置bean

package com.zt.spring;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; @Configuration
public class MyBeanConfig { @Bean
public CarFactoryBean createRunnableFactoryBean() {
return new CarFactoryBean();
} }

  

1.1.2.3 配置FactoryBean 生产bean

package com.zt.spring;

import org.springframework.beans.factory.FactoryBean;

public class CarFactoryBean implements FactoryBean<Car> {

    //获取到对应的实体
@Override
public Car getObject() throws Exception {
return new Car();
} // 返回的额对应的是class文件
@Override
public Class<?> getObjectType() {
return Car.class;
} //配置是不是单例
@Override
public boolean isSingleton() {
return true;
} }

  

1.1.2.4 获取上文,获取bean

package com.zt.spring;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class App {
public static void main(String[] args) {
// 获取上下文
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyBeanConfig.class);
// 通过FactoryBean 创建的bean
System.out.println(context.getBean(Car.class));
// 通过name获取car的实体
System.out.println(context.getBean("createRunnableFactoryBean"));
context.close();
}
}

  

1.1.3 通过另外一种工工厂模式实现bena的装配

1.1.3.1 创建一个bean

package com.zt.spring;

public class Jeep {
}

  

1.1.3.2 配置Config 配置bean

package com.zt.spring;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; @Configuration
public class MyBeanConfig { @Bean
public JeepFactory createJeepFactory() {
return new JeepFactory();
} @Bean
public Jeep createJeep(JeepFactory factory) {
return factory.creat();
}
}

  

1.1.3.3 创建 Factory 实现构建方法

package com.zt.spring;

public class JeepFactory {

    public Jeep creat() {
return new Jeep();
}
}

  

1.1.3.4 获取上文,获取bean

package com.zt.spring;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class App {
public static void main(String[] args) {
// 获取上下文
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyBeanConfig.class);
System.out.println(context.getBean("createJeep"));
System.out.println(context.getBean(Jeep.class));
context.close();
}
}

  

1.2 spring中bean的销毁方法

1.2.1 调用spring自己的初始化和销毁的方法

1.2.1.1 创建一个bean

并且继承InitializingBean, DisposableBean ,实现afterPropertiesSet,destroy方法

  

package com.zt.spring;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean; public class User implements InitializingBean, DisposableBean { @Override
public void afterPropertiesSet() throws Exception {
System.out.println("=======afterPropertiesSet========");
} @Override
public void destroy() throws Exception {
System.out.println("=======destroy========");
}
}

  

1.2.1.2在conffig文件中装配这个bean

package com.zt.spring;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; @Configuration
public class MyBeanConfig { @Bean
public User createUser() {
return new User();
}
}

  

1.2.1.3 获取上下文

package com.zt.spring;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class App {
public static void main(String[] args) {
// 获取上下文
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyBeanConfig.class);
System.out.println(context.getBean(User.class));
context.close();
}
}

  

1.2.2 调用自定义初始化和销毁的方法

1.2.2.1 创建一个bean

自定义init,destroy方法
package com.zt.spring;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; @Configuration
public class MyBeanConfig { @Bean(initMethod = "init",destroyMethod = "destroy")
public Dog createDog() {
return new Dog();
}
}

  

1.2.2.2在conffig文件中装配这个bean

package com.zt.spring;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; @Configuration
public class MyBeanConfig { @Bean(initMethod = "init",destroyMethod = "destroy")
public Dog createDog() {
return new Dog();
}
}

  

1.2.2.3 获取上下文

package com.zt.spring;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class App {
public static void main(String[] args) {
// 获取上下文
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyBeanConfig.class);
System.out.println(context.getBean(Dog.class));
context.close();
}
}

  

1.2.3 调用自定义并且注解初始化和销毁的方法

1.2.3.1 创建一个bean

自定义init,destroy方法  在加上注解放 @PostConstruct, @PreDestroy的方式实现
package com.zt.spring;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy; public class UserInfo { @PostConstruct
public void afterPropertiesSet() throws Exception {
System.out.println("=======afterPropertiesSet========");
} @PreDestroy
public void destroy() throws Exception {
System.out.println("=======destroy========");
}
}

  

1.2.3.2在conffig文件中装配这个bean

package com.zt.spring;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; @Configuration
public class MyBeanConfig {
@Bean
public UserInfo createUserInfo() {
return new UserInfo();
}
}

  

1.2.2.3 获取上下文

package com.zt.spring;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class App {
public static void main(String[] args) {
// 获取上下文
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyBeanConfig.class);
System.out.println(context.getBean(UserInfo.class));
context.close();
}
}

  

1.4 pom.文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com..zt</groupId>
<artifactId>spring</artifactId>
<version>1.0-SNAPSHOT</version> <properties>
<project.build.sourceEncodding>UTF-8</project.build.sourceEncodding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties> <dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.20.RELEASE</version>
</dependency>
</dependencies> </project>

  

最后

感谢你看到这里,说的都是自己的一些看法和见解,如有不对,请指正!觉得文章对你有帮助的话不妨给我点个赞,每天都会分享java相关技术文章或行业资讯,欢迎大家关注和转发文章!

java开发两年,连Spring中bean的装配都不知道?你怎么涨薪啊的更多相关文章

  1. Spring 中Bean的装配方式

    最近又买了一本介绍SSM框架的书,是由黑马程序员编写的,书上讲的很好理解,边看边总结一下.主要总结一下bean的装配方式. Bean的装配可以理解为依赖系统注入,Bean的装配方式即Bean依赖注入的 ...

  2. Spring笔记2——Spring中Bean的装配

    1.引言 Spring中,对象无需自己负责查找或创建与其关联的其他对象,而是由容器负责把需要相互协作的对象引用赋予各个对象.创建应用对象之间的协作关系的行为通常称为装配(Wiring),这也是依赖注入 ...

  3. 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring中Bean的作用域

    作用域的种类 Spring 容器在初始化一个 Bean 的实例时,同时会指定该实例的作用域.Spring3 为 Bean 定义了五种作用域,具体如下. 1)singleton 单例模式,使用 sing ...

  4. Spring中Bean的装配方式

    一.基于xml的装配 Student.java package com.yh; public class Student implements People { public void breath( ...

  5. spring中 Bean的装配 Bean后处理器

  6. 浅析Spring中bean的作用域

    一.前言   刚刚花了点时间,阅读了一下Spring官方文档中,关于bean的作用域这一块的内容.Spring-4.3.21官方文档中,共介绍了七种bean作用域,这篇博客就来简单介绍一下这七种作用域 ...

  7. JAVA面试题:Spring中bean的生命周期

    Spring 中bean 的生命周期短暂吗? 在spring中,从BeanFactory或ApplicationContext取得的实例为Singleton,也就是预设为每一个Bean的别名只能维持一 ...

  8. Spring中bean的配置

    先从IOC说起,这个概念其实是从我们平常new一个对象的对立面来说的,我们平常使用对象的时候,一般都是直接使用关键字类new一个对象,那这样有什么坏处呢?其实很显然的,使用new那么就表示当前模块已经 ...

  9. (转)Spring中Bean的命名问题(id和name区别)及ref和idref之间的区别

    Spring中Bean的命名 1.每个Bean可以有一个id属性,并可以根据该id在IoC容器中查找该Bean,该id属性值必须在IoC容器中唯一: 2.可以不指定id属性,只指定全限定类名,如: & ...

随机推荐

  1. 安装 Linux 系统基础知识概要

    虚拟化软件,建议使用 Vmware Workstation 虚拟硬件配置CPU:2核或更多内存:1G以上,推荐2G硬盘:一块硬盘,200G (虚拟大小)网卡:NAT模式 (桥接在外部网络变化时,无法访 ...

  2. Topsis优劣解距离法 mlx代码

    请参考https://blog.csdn.net/qq_36384657/article/details/98188769 mlx代码 topsis 优劣解距离法 参数说明: 分数.获奖次数.价值等 ...

  3. OpenCV计算机视觉学习(7)——图像金字塔(高斯金字塔,拉普拉斯金字塔)

    如果需要处理的原图及代码,请移步小编的GitHub地址 传送门:请点击我 如果点击有误:https://github.com/LeBron-Jian/ComputerVisionPractice 本节 ...

  4. 前端基础——HTML(一)

    HTML html超文本标记语言 前端三层 HTML结构层 css样式层 JavaScript行为层 其他多媒体内容(图片,音频等等) 互联网运行过程 客 --http请求--> 服 户 htt ...

  5. Java 第四课 对象 类

    1.构造方法可以为private public 2.抽象类可以有构造方法,但是必须在子类中调用(super.构造方法)

  6. 字体图标:Font Awesome

    小图标 Font Awesome Font Awesome 字体为您提供可缩放矢量图标,它可以被定制大小.颜色.阴影以及任何可以用 CSS 的样式,是一款惊艳的字体图标! 可以前往官网进行学习 Fon ...

  7. 关于天线长度及LC值的计算

    一.天线长度与波长 1.天线最佳长度计算 理论和实践证明,当天线的长度为无线电信号波长的1/4时,天线的发射和接收转换效率最高.因此,天线的长度将根据所发射和接收信号的频率即波长来决定.只要知道对应发 ...

  8. 数据结构(C++)——链栈

    结点结构 typedef char ElemType; typedef struct LkStackNode{ ElemType data; LkStackNode *next; }*Stack,SN ...

  9. python接口自动化测试--批量读取数据

    为了便于维护,python接口自动化测试用例可以利用xlrd模块读取excal表格进行数据分离.我们可以利用xlrd模块的row_values()和cell_value()两种方法读取Excal表格. ...

  10. LuoguP4704 太极剑

    题面 测试要求 Bob 尽可能快地切断 n 根绳子. 所有绳子的端点两两不同,所以共有 2n 个端点.这些端点被捆在一个圆上,等距离分布.我们把这些端点按顺时针方向编号为 1 到 2n. Bob 每次 ...