首页 > 其他分享 >用Spring工厂实例化对象

用Spring工厂实例化对象

时间:2022-11-22 12:08:34浏览次数:36  
标签:www http Spring springframework 工厂 实例 spring org schema


如何导入spring相关包,以及基本的配置及应用spring工厂

spring的作用有多大,它代表的不仅仅是spring框架本身,在前辈的更新下,已经能无脑支持structs , mybatis, hibernate等各种框架了,想要使用各种框架的码宅们只需要引入相关依赖包就能直接使用各种框架了。
本文旨在教会新手们如何引入相关依赖包,以及在引包时应该注意的地方,并且简单地用spring自带的工厂,来做一个小小的应用示范。

主包

spring-core-3.2.4.RELEASE.jar

按需引包

spring-aop-3.2.4.RELEASE.jar

spring-aspects-3.2.4.RELEASE.jar

spring-beans-3.2.4.RELEASE.jar

spring-build-src-3.2.4.RELEASE.jar

spring-context-3.2.4.RELEASE

spring-context-support-3.2.4.RELEASE.jar


部分依赖包

commons-logging-1.0.3.jar


spring3的相关包哪里有下载呢,csdn-下载-搜索里太多了,笔者不多谈。

引包

1.准备好需要用到的jar包

用Spring工厂实例化对象_spring

2.全选,复制,在Myeclipse的新工程下,新建一个lib的文件夹

用Spring工厂实例化对象_jar_02


然后粘贴进去。3.把这个lib下的jar包添加进引用,右键工程-build path-Libraries-add external jars- 选中工程路径下lib文件夹里你刚刚粘贴进去的包-确定

用Spring工厂实例化对象_spring_03

这里笔者只引了一部分!

这里有人可能对lib比较疑惑,新建的lib文件夹不会被自动引入的,必须通过手动引入,就是上面的第三步,工程路径怎么找,右键工程-properties-选中:

用Spring工厂实例化对象_jar_04


看,Location就是工程路径,里面就有我们刚刚新建的lib文件夹。

代码

主类:
package com;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainClass {
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
BeanFactory bf =new ClassPathXmlApplicationContext("applicationContext.xml");
IMessage msg = (IMessage)bf.getBean("msg");
msg.getMessage();
}
}

Message类:
package com;

public class Message implements IMessage {

public void getMessage(){
System.out.println("info");
}
}

IMessage接口:
package com;

public interface IMessage {

public abstract void getMessage();

}

然后看一下配置文件applicationContext.xml
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

<bean id="msg" class="com.Message"></bean>

</beans>

配置文件里,上面的写法引用都是固定的,下面只添加了一个id=”msg”的bean,这是为什么主类可以通过bf.getBean(“msg”)获取到实例。
输出结果就是: info
不做附图了。

笔者在引用jar包的时候是先放入一个新建的lib,再把他添加到libraries里面去,如果大家嫌懒,而且工程正好又是一个web工程,那就可以直接把包丢进WEB-INFO的lib里面去,就不需要上述的添加步骤了。

用Spring工厂实例化对象_jar_05


复制粘贴进去以后,使用需要的包的时候,只要ctrl+shift+o就行。


标签:www,http,Spring,springframework,工厂,实例,spring,org,schema
From: https://blog.51cto.com/u_11553781/5877550

相关文章

  • 实例066 如何确定程序的运行时间
      usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usi......
  • Spring Boot Admin 入门及配置
    什么是SpringBootAdmin?SpringBootAdmin是一个管理和监控SpringBoot应用的社区项目。创建SpringBootAdminServer要做到这一点,只需创建一个简单的SpringBo......
  • springboot的@EnableAutoConfigurationProperties注解
    一、创建一个springboot工程添加依赖,编写启动类,二、使用@Component注解让@ConfigurationProperties注解生效,从而完成配置文件与javaBean的数据绑定1、配置文件zs.nam......
  • Java:Spring Boot整合mybatis-plus示例
    文档https://start.spring.io/mybatis-plus文档|githubmaven文档p6spy文档|github使用示例(目录)项目结构$tree-Itarget.├──pom.xml└──src......
  • SpringMVC
    SpringMVCDispatcherServlet<servlet>    <servlet-name>springMVC</servlet-name>    <servlet-class>org.springframework.web.servlet.DispatcherSe......
  • Spring
    1、Spring1.1、简介Spring:春天----》给软件行业带来了春天2002年,首次推出了Spring框架的雏形:interface21框架Spring框架即以interface21框架为基础,经过重新设计,......
  • Springcloud学习笔记52--通过ApplicationContextAware接口从spring上下文中获取到需要
    1.背景在spring项目中,bean之间的依赖关系是spring容器自动管理的,但是一个项目中有些类不在spring容器中却需要使用spring管理的bean,这时候不能通过正常的方式(注解等方式)......
  • 基于Spring-AOP的自定义分片工具
    作者:陈昌浩1背景随着数据量的增长,发现系统在与其他系统交互时,批量接口会出现超时现象,发现原批量接口在实现时,没有做分片处理,当数据过大时或超过其他系统阈值时,就会出现......
  • Spring MVC之Converter类型转换器
    SpringMVC框架的Converter<S,T>是一个可以将一种数据类型转换成另一种数据类型的接口,这里S表示源类型,T表示目标类型。开发者在实际应用中使用框架内置的类型转换器基......
  • Spring Security(2)
    您好,我是湘王,这是我的博客园,欢迎您来,欢迎您再来~ 前面已经把需要的环境准备好了,包括数据库和SQL语句,现在再来写代码。至于安装MySQL什么的就跳过去了,娘度子里面一大把。......