通过静态工厂的方法创建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. SpringBoot+AOP构建多数据源的切换实践

    针对微服务架构中常用的设计模块,通常我们都会需要使用到druid作为我们的数据连接池,当架构发生扩展的时候 ,通常面对的数据存储服务器也会渐渐增加,从原本的单库架构逐渐扩展为复杂的多库架构. 当在业务 ...

  2. Mac OS X 10.13上 安装odoo 11.0开发环境

    0.准备假设homebrew已经安装好没安装的需要先安装,见下面链接http://brew.sh/ 1.安装PostgreSQL$ brew tap homebrew/services$ brew i ...

  3. 简说Python之flask-SQLAlchmey的web应用

    目录 原生语句操作MySQL数据库 1.安装MySQL 2.MySQL设置用户和权限 3.用PyMySQL操纵MySQL数据库 4. CRUD增,删,改,查 使用SQLAlchemy 1.安装SQLA ...

  4. C# MP3播放帮助类

    本文为原创文章如需转载请注明出处: /// <summary> /// ************************************************* /// 类名:M ...

  5. Python - 面向对象(二)类方法、静态方法

    面向对象的各种方法 静态方法 - @staticmethod class Person(): name = "cool guy" @staticmethod def static( ...

  6. centos 7 中没有iptables 和service iptables save 指令使用失败问题解决方案

    1.任意运行一条iptables防火墙规则配置命令: iptables -P OUTPUT ACCEPT 2.对iptables服务进行保存: service iptables save 如果上述命令 ...

  7. 如何将自己的测试脚本分离成PO模式的测试框架

    1 PO模式 1.1 PO模式介绍 Page Object Model 测试页面和测试脚本分离,即页面封装成类,供测试脚本调用. (将项目分为page.py和test.py) 测试用例:就是excel ...

  8. Unity 游戏框架:UI 管理神器 UI Kit

    UI Kit 快速入门 首先我们来进行 UI Kit 的快速入门 制作一个界面的,步骤如下: 准备 生成代码 逻辑编写 运行 1. 准备 先创建一个场景 TestUIHomePanel. 删除 Hie ...

  9. python使用pip安装模块出现ReadTimeoutError: HTTPSConnectionPool的解决方法

    今天使用pip安装第三库时,有时会报错: pip._vendor.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='file ...

  10. 数据库-Mysql语句

    Mysql语句 不定时更新 1.数据库相关的语句 /* 数据库操作 */ ------------------ -- 启动MySQL net start mysql -- 接与断开服务器 mysql ...