Spring4.0注入bean的新方法

2024-08-21 07:38
文章标签 spring4.0 bean 注入 方法

本文主要是介绍Spring4.0注入bean的新方法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

主要介绍spring4.0的自动扫描功能,以及对bean的过滤等特性进行学习。

package com.herman.ss.test;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import com.herman.ss.action.TestAction;
import com.herman.ss.filter.Filter1;
import com.herman.ss.filter.Filter2;
import com.herman.ss.filter.test.Filter3;
import com.herman.ss.pojo.House;
import com.herman.ss.pojo.Person;
/*** @see spring4.0.0最新稳定版新特性,自动扫描bean,自动注入bean* @author Herman.Xiong* @date 2014年7月18日14:49:42*/
public class Test1 {/*** @see spring4.0自动扫描bean,自动注入bean*/public static void test0(){//1.加载配置文件ApplicationContext ctx=new ClassPathXmlApplicationContext("com/herman/ss/config/applicationContext1.xml");//2.获取bean实例Person person=(Person)ctx.getBean("person");House house=(House)ctx.getBean("house");//3.打印bean属性System.out.println(person);System.out.println(house);}/*** @see spring4.0简单业务逻辑的注解*/public static void test1(){//1.加载配置文件ApplicationContext ctx=new ClassPathXmlApplicationContext("com/herman/ss/config/applicationContext1.xml");//2.获取bean实例 获取beanTestAction testAction=(TestAction)ctx.getBean("testAction");//3.打印bean属性System.out.println(testAction);//4.调用bean对象的方法testAction.testAction();//@Service 用于标注业务层组件;//@Repository 用于标注数据访问层组件;//@Controller 用于标注控制层组件(如:Struts中的action)//@Component 表示泛型组件,当组件不好归类的时候,我们可以使用这个组件进行注解。}/*** @see spring4.0简单注解的排除过滤器配置*/public static void test2(){//1.加载配置文件ApplicationContext ctx=new ClassPathXmlApplicationContext("com/herman/ss/config/applicationContext1.xml");//2.获取bean实例,只能根据bean的id获取beanFilter1 filter1=(Filter1)ctx.getBean("filter1");Filter2 filter2=(Filter2)ctx.getBean("filter2");//3.打印bean属性System.out.println(filter1);System.out.println(filter2);/*** 运行会报错:Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'filter2' is defined* 原因是:filter2被我们排除在外了,不会自动注入* 因此会抛异常*/}/*** @see spring4.0简单注解的包含过滤器配置*/public static void test3(){//1.加载配置文件ApplicationContext ctx=new ClassPathXmlApplicationContext("com/herman/ss/config/applicationContext1.xml");//2.获取bean实例	Filter3 filter3=(Filter3)ctx.getBean("filter3");Filter2 filter2=(Filter2)ctx.getBean("filter2");Filter1 filter1=(Filter1)ctx.getBean("filter1");//3.打印bean属性System.out.println(filter3);System.out.println(filter2);System.out.println(filter1);/*** 运行会报错:Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'filter2' is defined* 原因:filter2 被我们排除在外了* 因此:我们回去filter2  这个bean对象的时候就会报错。* filter1  为什么不报错呢,因为我们设置了 com.herman.ss.filter包下面的use-default-filters="true"  自动导入* 因此:filter1 不会报错*/}public static void main(String[] args) {/*** 注解需要的jar包列举:* spring-aop-4.0.6.RELEASE.jar* spring-beans-4.0.6.RELEASE.jar* spring-context-4.0.6.RELEASE.jar* spring-core-4.0.6.RELEASE.jar* spring-expression-4.0.6.RELEASE.jar* commons-lang-2.4.jar*///test0();//test1();//test2();test3();}
}

配置文件源码:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"  xmlns:mvc="http://www.springframework.org/schema/mvc"  xmlns:context="http://www.springframework.org/schema/context"  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-4.0.xsd  http://www.springframework.org/schema/mvc  http://www.springframework.org/schema/mvc/spring-mvc.xsd  http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-4.0.xsd"><!-- 打开Spring组件自动扫面,并配置要扫描的基本包 -->  <context:component-scan base-package="com.herman.ss.pojo"></context:component-scan><context:component-scan base-package="com.herman.ss.action"></context:component-scan><context:component-scan base-package="com.herman.ss.biz"></context:component-scan><context:component-scan base-package="com.herman.ss.dao"></context:component-scan><context:component-scan base-package="com.herman.ss.filter" use-default-filters="false"><!-- 取消自动注入,配置只注入com.herman.ss.filter.test下面的所有类 --><context:include-filter type="regex" expression="com.herman.ss.filter.test.*"/></context:component-scan><context:component-scan base-package="com.herman.ss.filter" use-default-filters="true"><!-- 自动注入,但是Filter2除外 --><context:exclude-filter type="regex" expression="com.herman.ss.filter.Filter2" /> </context:component-scan><!-- 注:<context:component-scan>节点用于通知Spring容器扫描组件,base-package属性用于指定将要被扫描的组件所在的包名这里将自动的配置扫描com.herman.ss.pojo下面的bean-->
</beans>

实体类源码:

package com.herman.ss.pojo;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/*** @see 实体类使用Component注解* @author Herman.Xiong* @date 2014年7月24日17:11:59*/
@Component("person")
public class Person {private String name;private int age;//这里设置自动注入@Autowiredprivate House house;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public House getHouse() {return house;}public void setHouse(House house) {this.house = house;}public Person() {super();}public Person(String name, int age) {super();this.name = name;this.age = age;}public Person(String name, int age, House house) {super();this.name = name;this.age = age;this.house = house;}@Overridepublic String toString() {return "Person [age=" + age + ", house=" + house + ", name=" + name+ "]";}}

House.java源码:

package com.herman.ss.pojo;import org.springframework.stereotype.Component;@Component("house")
public class House {private String name;private String address;private float price;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}public float getPrice() {return price;}public void setPrice(float price) {this.price = price;}public House() {super();}public House(String name, String address, float price) {super();this.name = name;this.address = address;this.price = price;}public String toString() {return "House [address=" + address + ", name=" + name + ", price="+ price + "]";}}
package com.herman.ss.action;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;import com.herman.ss.biz.TestBiz;/*** @see 模拟action* @author Herman.Xiong* @date 2014年7月24日17:17:16* @since jdk 1.6,tomcat 6.0*/
@Controller("testAction")
public class TestAction {//使用自动载入@Autowiredprivate TestBiz testBiz;//必须提供set方法public void setTestBiz(TestBiz testBiz) {this.testBiz = testBiz;}public TestAction(){System.out.println("模拟的action类");}public void testAction(){testBiz.testBiz();}
}
package com.herman.ss.biz;/*** @see 模拟biz层进行注解* @author Herman.Xiong* @date 2014年7月24日17:20:25*/
public interface TestBiz {void testBiz();
}
package com.herman.ss.biz.impl;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import com.herman.ss.biz.TestBiz;
import com.herman.ss.dao.TestDao;
@Service("testBiz")
public class TestBizImpl implements TestBiz{@Autowiredprivate TestDao testDao;//必须提供set方法public void setTestDao(TestDao testDao) {this.testDao = testDao;}public void testBiz() {System.out.println("模拟biz层");testDao.testDao();}}
package com.herman.ss.dao;/*** @see 模拟dao层进行注解* @author Herman.Xiong* @date 2014年7月24日17:20:25*/
public interface TestDao {void testDao();
}
package com.herman.ss.dao.impl;import org.springframework.stereotype.Repository;import com.herman.ss.dao.TestDao;
@Repository("testDao")
public class TestDaoImpl implements TestDao{public void testDao() {System.out.println("模拟dao层");}}
package com.herman.ss.filter;import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
//Scope注解设置作用域
@Controller("filter1")@Scope("prototype")
public class Filter1 {public Filter1(){System.out.println("我是Filter1 ...");System.out.println("Scope注解设置作用域");}
}
package com.herman.ss.filter.test;import org.springframework.stereotype.Controller;@Controller("filter3")
public class Filter3 {public Filter3(){System.out.println("我是filter3");}
}
package com.herman.ss.filter;import org.springframework.stereotype.Controller;@Controller("filter2")
public class Filter2 {public Filter2(){System.out.println("我是Filter2 ...");}
}

 

原网页地址:http://www.itnose.net/detail/6078862.html#0-youdao...

这篇关于Spring4.0注入bean的新方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1092540

相关文章

【C++】_list常用方法解析及模拟实现

相信自己的力量,只要对自己始终保持信心,尽自己最大努力去完成任何事,就算事情最终结果是失败了,努力了也不留遗憾。💓💓💓 目录   ✨说在前面 🍋知识点一:什么是list? •🌰1.list的定义 •🌰2.list的基本特性 •🌰3.常用接口介绍 🍋知识点二:list常用接口 •🌰1.默认成员函数 🔥构造函数(⭐) 🔥析构函数 •🌰2.list对象

浅谈主机加固,六种有效的主机加固方法

在数字化时代,数据的价值不言而喻,但随之而来的安全威胁也日益严峻。从勒索病毒到内部泄露,企业的数据安全面临着前所未有的挑战。为了应对这些挑战,一种全新的主机加固解决方案应运而生。 MCK主机加固解决方案,采用先进的安全容器中间件技术,构建起一套内核级的纵深立体防护体系。这一体系突破了传统安全防护的局限,即使在管理员权限被恶意利用的情况下,也能确保服务器的安全稳定运行。 普适主机加固措施:

webm怎么转换成mp4?这几种方法超多人在用!

webm怎么转换成mp4?WebM作为一种新兴的视频编码格式,近年来逐渐进入大众视野,其背后承载着诸多优势,但同时也伴随着不容忽视的局限性,首要挑战在于其兼容性边界,尽管WebM已广泛适应于众多网站与软件平台,但在特定应用环境或老旧设备上,其兼容难题依旧凸显,为用户体验带来不便,再者,WebM格式的非普适性也体现在编辑流程上,由于它并非行业内的通用标准,编辑过程中可能会遭遇格式不兼容的障碍,导致操

透彻!驯服大型语言模型(LLMs)的五种方法,及具体方法选择思路

引言 随着时间的发展,大型语言模型不再停留在演示阶段而是逐步面向生产系统的应用,随着人们期望的不断增加,目标也发生了巨大的变化。在短短的几个月的时间里,人们对大模型的认识已经从对其zero-shot能力感到惊讶,转变为考虑改进模型质量、提高模型可用性。 「大语言模型(LLMs)其实就是利用高容量的模型架构(例如Transformer)对海量的、多种多样的数据分布进行建模得到,它包含了大量的先验

【北交大信息所AI-Max2】使用方法

BJTU信息所集群AI_MAX2使用方法 使用的前提是预约到相应的算力卡,拥有登录权限的账号密码,一般为导师组共用一个。 有浏览器、ssh工具就可以。 1.新建集群Terminal 浏览器登陆10.126.62.75 (如果是1集群把75改成66) 交互式开发 执行器选Terminal 密码随便设一个(需记住) 工作空间:私有数据、全部文件 加速器选GeForce_RTX_2080_Ti

【VUE】跨域问题的概念,以及解决方法。

目录 1.跨域概念 2.解决方法 2.1 配置网络请求代理 2.2 使用@CrossOrigin 注解 2.3 通过配置文件实现跨域 2.4 添加 CorsWebFilter 来解决跨域问题 1.跨域概念 跨域问题是由于浏览器实施了同源策略,该策略要求请求的域名、协议和端口必须与提供资源的服务相同。如果不相同,则需要服务器显式地允许这种跨域请求。一般在springbo

AI(文生语音)-TTS 技术线路探索学习:从拼接式参数化方法到Tacotron端到端输出

AI(文生语音)-TTS 技术线路探索学习:从拼接式参数化方法到Tacotron端到端输出 在数字化时代,文本到语音(Text-to-Speech, TTS)技术已成为人机交互的关键桥梁,无论是为视障人士提供辅助阅读,还是为智能助手注入声音的灵魂,TTS 技术都扮演着至关重要的角色。从最初的拼接式方法到参数化技术,再到现今的深度学习解决方案,TTS 技术经历了一段长足的进步。这篇文章将带您穿越时

Spring 源码解读:自定义实现Bean定义的注册与解析

引言 在Spring框架中,Bean的注册与解析是整个依赖注入流程的核心步骤。通过Bean定义,Spring容器知道如何创建、配置和管理每个Bean实例。本篇文章将通过实现一个简化版的Bean定义注册与解析机制,帮助你理解Spring框架背后的设计逻辑。我们还将对比Spring中的BeanDefinition和BeanDefinitionRegistry,以全面掌握Bean注册和解析的核心原理。

模版方法模式template method

学习笔记,原文链接 https://refactoringguru.cn/design-patterns/template-method 超类中定义了一个算法的框架, 允许子类在不修改结构的情况下重写算法的特定步骤。 上层接口有默认实现的方法和子类需要自己实现的方法

使用JS/Jquery获得父窗口的几个方法(笔记)

<pre name="code" class="javascript">取父窗口的元素方法:$(selector, window.parent.document);那么你取父窗口的父窗口的元素就可以用:$(selector, window.parent.parent.document);如题: $(selector, window.top.document);//获得顶级窗口里面的元素 $(