通过静态工厂的方法创建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 ,注意不是配置静态工厂的实例,而是配置bean的实例 -->
<!-- class 属性:指向的是静态工厂方法的全类名
factory-method :指向的是静态工厂方法的名字
constructor-arg:如果工厂方法需要传入参数,则使用constructor-arg来配置参数 -->
<bean id="car" class="lib2.Car"
factory-method="gerCae">
<constructor-arg value="audi"></constructor-arg></bean>
<!-- 配置工厂的实例 -->
<bean id="casec" class="instance"></bean>
<!-- 通过实例工厂方法来配置bean -->
<!-- factory-bean: 属性:指向的是静态工厂方法的bean
factory-method :指向的是静态工厂方法的名字
constructor-arg:如果工厂方法需要传入参数,则使用constructor-arg来配置参数 -->
<bean id="cae" factory-bean="cad" factory-method="cac">
<constructor-arg value="asca"></constructor-arg></bean> </beans>

factory来配置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">
<!--
通过factorybean来配置bean的实例
class:只想factorybean的全类名
property:配置的事factorybean的属性(可能是里面的复制函数set) -->
<bean id="Car" class="factorybean">
<property name="brand" value="bwm"></property></bean> </beans>

基于注解来配置bean和bean的线相关的属性。

组件扫描:spring能够从classpath夏自动扫描、侦测和实例化具有特定注释的组件;

package test1;

import org.springframework.stereotype.Component;

@Component
public class TestObject { }
package test2; import org.springframework.stereotype.Repository; @Repository
public class UserReposityImpl implements UserRepository { @Override
public void save() {
// TODO Auto-generated method stub
System.out.println("UserRepository save...."); }
}package test3; import org.springframework.stereotype.Service; @Service
public class UserService {
public void add()
{
System.out.println("UserSerivce add..");
} }
package test4; import org.springframework.stereotype.Controller; @Controller
public class UserController {
public void execute()
{
System.out.println("UserController execute..");
}
}
<?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-4.3.xsd">
<!-- 指定spring ioc容器扫描的包 -->
<context:component-scan base-package="test1.TestObject"></context:component-scan> </beans>
package test1; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import test2.UserRepository;
import test3.UserService;
import test4.UserController; public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext ct=new ClassPathXmlApplicationContext("beans-053.xml");
TestObject to=(TestObject) ct.getBean("testObject");
UserController userController=(UserController) ct.getBean("userController");
UserRepository userRepository=(UserRepository) ct.getBean("userRepository");
UserService userService=(UserService) ct.getBean("userService");
System.out.println(to);
System.out.println(userController);
System.out.println(userService);
System.out.println(userRepository); }
}

关于MyBeanPostProcessor的应用。

spring05的更多相关文章

  1. Spring-05 使用注解开发

    Spring-05 使用注解开发 使用注解开发 1.项目准备 在spring4之后,想要使用注解形式,必须得要引入aop的包5 <!-- https://mvnrepository.com/ar ...

  2. spring05配置文件之间的关系

    一:配置文件包含关系 1.创建对应的实体类 public class Student { //学生实体类 private String name; //姓名 private Integer age; ...

  3. Spring-05 -AOP [面向切面编程] -Schema-based 实现aop的步骤

    一.AOP [知识点详解] AOP:中文名称面向切面编程 英文名称:(Aspect Oriented Programming) 正常程序执行流程都是纵向执行流程 3.1 又叫面向切面编程,在原有纵向执 ...

  4. Spring05——Spring 如何实现事务管理

    在此之前,我们已经了解了 Spring 相关的基础知识,今天将为给位带来,有关 Spring 事务代理的相关知识.关注我的公众号「Java面典」,每天 10:24 和你一起了解更多 Java 相关知识 ...

  5. 玩转spring boot——结合JPA入门

    参考官方例子:https://spring.io/guides/gs/accessing-data-jpa/ 接着上篇内容 一.小试牛刀 创建maven项目后,修改pom.xml文件 <proj ...

  6. Spring:AOP面向切面编程

    AOP主要实现的目的是针对业务处理过程中的切面进行提取,它所面对的是处理过程中的某个步骤或阶段,以获得逻辑过程中各部分之间低耦合性的隔离效果. AOP是软件开发思想阶段性的产物,我们比较熟悉面向过程O ...

  7. Spring实现Ioc的多种方式--控制反转、依赖注入、xml配置的方式实现IoC、对象作用域

    Spring实现Ioc的多种方式 一.IoC基础 1.1.概念: 1.IoC 控制反转(Inversion of Control) IoC是一种设计思想. 2.DI 依赖注入 依赖注入是实现IoC的一 ...

  8. Spring4学习回顾之路10-Spring4.x新特性:泛型依赖注入

    泛型依赖注入:Spring 4.x中可以为子类注入子类对应的泛型类型的成员变量的引用. 话语太过抽象,直接看代码案例,依次建立如下代码: User.java package com.lql.sprin ...

  9. 环绕通知(xml)

    1.maven依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="h ...

随机推荐

  1. Python面向对象之反射,双下方法

    一. 反射 反射的概念是由Smith在1982年首次提出的,主要是指程序可以访问.检测和修改它本身状态或行为的一种能力(自省).这一概念的提出很快引发了计算机科学领域关于应用反射性的研究.它首先被程序 ...

  2. linux php 安装libiconv过程与总结

    问题:在嵌入式linux 已经安装好的php的情景下,需要安装一个扩展库libiconv 背景:从后台传的数据含有中文(gbk2312)的通过json_encode 显示为null,查阅资料发现jso ...

  3. tersserorc的简单使用

    tesserocr 是 python 的一个 OCR 库,它是对 tesseract 做的一层 Python API 封装,所以他的核心是tesseract. tesseract 的安装见 https ...

  4. Java并发编程学习前期知识下篇

    Java并发编程学习前期知识下篇 通过上一篇<Java并发编程学习前期知识上篇>我们知道了在Java并发中的可见性是什么?volatile的定义以及JMM的定义.我们先来看看几个大厂真实的 ...

  5. Python - 面向对象(三)公共变量,受保护变量,私有变量

    前言 在Python的类里面,所有属性和方法默认都是公共的:但Python也可以设置受保护.私有类型的变量or方法 受保护类型的变量.方法 一般称为:protected变量 #!/usr/bin/en ...

  6. SpringMVC框架——自定义数据类型转换器

    Spring MVC 框架的 Converter<S,T> 是一个可以将一种数据类型转换成另一种数据类型的接口,这里 S 表示源类型,T 表示目标类型. 开发中如果需要自定义数据类型转换时 ...

  7. 大多数人不知道的表格其他写法的onmouseover效果

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...

  8. GitHub 热点速览 Vol.13:近 40k star 计算机论文项目再霸 GitHub Trending 榜

    作者:HelloGitHub-小鱼干 摘要:"潮流是个轮回",这句话用来形容上周的 GitHub Trending 最贴切不过.无论是已经获得近 40k 的高星项目 Papers ...

  9. spring-boot-plus-v2.0发布了-让天下没有难写的代码

    spring-boot-plus是易于使用,快速,高效,功能丰富,开源的spring boot脚手架 前后端分离,专注于后端服务 目标 每个人都可以独立.快速.高效地开发项目! GITHUB | GI ...

  10. [UWP]使用AlphaMaskEffect提升故障艺术动画的性能(顺便介绍怎么使用性能探测器分析UWP程序)

    前几天发布了抄抄<CSS 故障艺术>的动画这篇文章,在这篇文章里介绍了如何使用Win2D绘制文字然后配合BlendEffect制作故障艺术的动画.本来打算就这样收手不玩这个动画了,但后来又 ...