主要是学会spring中关于通过注释来配置bean。

以及处理怎样智选其一或者补选某一个bean还有就是关于当出现多个相同的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"
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容器扫描的包 -->
<!-- 通过resource-pattern来指定扫描的资源 -->
<!-- <context:component-scan base-package="test1"
resource-pattern="test1.user/*.class">
</context:component-scan>-->
<context:component-scan base-package="test1" use-default-filters="true">
<!-- <context:exclude-filter type="annotation" expression="baoing"/>指定排除那些那些指定的表达式的组件-->
<!-- <context:include-filter type="annotation" expression="baoming "只包含指定的组件,需要将相应use-default-filters="false"的贼之改成false-->
<!-- <context:exclude-filter type="assignable" expression="jikoubaomingjialeiming "/>-->
</context:component-scan> </beans>

然后就是相应的g各个bean文件以及上边相应的注释。;

注意一定要放到相应的子包下下面。

package test1;

import org.springframework.stereotype.Component;

@Component
public class TestObject { }
package test1.user;

public interface UserRepository {
void save();
}
package test1.user;

import org.springframework.stereotype.Repository;

@Repository("userRepository")
public class UserReposityImpl implements UserRepository { @Override
public void save() {
// TODO Auto-generated method stub
System.out.println("UserRepository save...."); }
}
package test1;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import test1.cont.UserController;
import test1.ser.UserService;
import test1.user.UserRepository; public class Spring { 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);
} }
package test1.cont;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller; import test1.ser.UserService; @Controller
public class UserController {
@Autowired
private UserService userService;
public void execute()
{
System.out.println("UserController execute..");
userService.add();
}
}
package test1.ser;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service; import test1.user.UserRepository;
//@Autowired(required=false)设置没有spring装备的bean
//@Qualifier("zhidingde bean")设置指定的bean
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
public void add()
{
System.out.println("UserSerivce add..");
userRepository.save();
} }

实验结果如下:

spring06的更多相关文章

  1. Spring-06 AOP

    Spring-06 AOP AOP 1.简介 AOP(Aspect Oriented Programming)意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术. AO ...

  2. :Spring-06 -AOP [面向切面编程] -配置异常通知的两种方式--AspectJ 方式 -Schema-based 方式

    三.配置异常通知的步骤(AspectJ 方式) 1.只有当切点报异常才能触发异常通知 2.在spring 中有AspectJ 方式提供了异常通知的办法 3.实现步骤: 3.1新建类,在类写任意名称的方 ...

  3. 玩转spring boot——结合JPA事务

    接着上篇 一.准备工作 修改pom.xml文件 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=&q ...

  4. 二)Spring AOP编程思想与动态代理

    一.aop编程思想 1.面向切面,就是能够不动源码的情况下,从横切面切入新的代码功能. 2.实现原理是动态代理 动态代理的步骤 a.写生产厂家,实现接口,代理只能代理接口 b.动态代理类实现Invoc ...

  5. Spring:AOP面向切面编程

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

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

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

  7. spring中JdbcTemplate使用

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

随机推荐

  1. C语言程序设计(七) 函数

    第七章 函数 分而治之: 把较大的任务分解成若干较小.较简单的任务,并提炼出公用任务的方法 函数是C语言中模块化程序设计的最小单位,既可以把每个函数都看作一个模块,也可以将若干相关的函数合并成一个模块 ...

  2. C++ for循环练习

    #include <stdio.h> //题目:现有公鸡5元一只,母鸡3元一只,小鸡1元3只(小鸡不能单买 最少3只起卖),此时顾客有100元,请问有多少种购买方案. int main() ...

  3. Journal of Proteome Research | Prediction of an Upper Limit for the Fraction of Interprotein Cross-Links in Large-Scale In Vivo Cross-Linking Studies (分享人:张宇星)

    题目:Prediction of an Upper Limit for the Fraction of Interprotein Cross-Links in Large-Scale In Vivo ...

  4. 【转】不怕难之BlockingQueue及其实现

    1. 前言 BlockingQueue即阻塞队列,它是基于ReentrantLock,依据它的基本原理,我们可以实现Web中的长连接聊天功能,当然其最常用的还是用于实现生产者与消费者模式,大致如下图所 ...

  5. 跟面试官侃半小时MySQL事务隔离性,从基本概念深入到实现

    提到MySQL的事务,我相信对MySQL有了解的同学都能聊上几句,无论是面试求职,还是日常开发,MySQL的事务都跟我们息息相关. 而事务的ACID(即原子性Atomicity.一致性Consiste ...

  6. hdu2642二维树状数组,单点修改+区间查询

    题目链接:http://icpc.njust.edu.cn/Problem/Hdu/2642/ 代码如下: #include<bits/stdc++.h> using namespace ...

  7. PHP session反序列化

    先来了解一下关于session的一些基础知识 什么是session 在计算机中,尤其是在网络应用中,称为“会话控制”.Session 对象存储特定用户会话所需的属性及配置信息.这样,当用户在应用程序的 ...

  8. [贪心]Codeforces Equal Rectangles

    Equal Rectangles time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  9. stm32CubeMx+TrueSTUDIO+uc/os-III移植开发(一)

    自从接触到stm32cubemx后,发现使用起来很方便,用来做项目开发的话,由于是图形化操作界面,工作效率比较快.如果要学习操作系统,以前的IDE如keil,IAR等IDE对操作系统的代码调试起来不够 ...

  10. 使用Keras进行深度学习:(一)Keras 入门

    欢迎大家关注我们的网站和系列教程:http://www.tensorflownews.com/,学习更多的机器学习.深度学习的知识! Keras是Python中以CNTK.Tensorflow或者Th ...