欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

第65节:Java后端的学习之Spring基础

程序员文章站 2022-06-03 22:40:16
Java后端的学习之Spring基础 如果要学习 ,那么什么是框架, 又是什么呢?学习 中的 和`bean aop IOC Bean AOP api springFramework`. 各种学习的知识点: 网站: 是一种开源框架,是为了解决企业应用开发的复杂性问题而创建的,现在的发展已经不止于用于企 ......

第65节:Java后端的学习之Spring基础

java后端的学习之spring基础

如果要学习spring,那么什么是框架,spring又是什么呢?学习spring中的iocbean,以及aop,ioc,bean,aop,(配置,注解,api)-springframework.

第65节:Java后端的学习之Spring基础

各种学习的知识点:

spring expression language
spring integration
spring web flow
spring security
spring data
spring batch

spring网站:
http://spring.io/

第65节:Java后端的学习之Spring基础

http://spring.io/projects/spring-framework
第65节:Java后端的学习之Spring基础

spring是一种开源框架,是为了解决企业应用开发的复杂性问题而创建的,现在的发展已经不止于用于企业应用了.

spring是一种轻量级的控制反转(ioc)和面向切面(aop)的容器框架.

一句名言:spring带来了复杂的javaee开发的春天.

jdbc orm
oxm jms
transactions
websocket servlet
web portlet
aop aspects instrumentation messaging
beans core context spel

springmvc+spring+hibernate/ibatis->企业应用

什么是框架,为什么要用框架:

什么是框架:

第65节:Java后端的学习之Spring基础

第65节:Java后端的学习之Spring基础

框架就是别人制定好的一套规则和规范,大家在这个规范或者规则下进行工作,可以说,别人盖好了楼,让我们住.

第65节:Java后端的学习之Spring基础

第65节:Java后端的学习之Spring基础

软件框架是一种半成品,具有特定的处理流程和控制逻辑,成熟的,可以不断升级和改进的软件.

使用框架重用度高,开发效率和质量的提高,容易上手,快速解决问题.

spring ioc容器

第65节:Java后端的学习之Spring基础

接口,是用于沟通的中介物的,具有抽象化,java中的接口,就是声明了哪些方法是对外公开的.

面向接口编程,是用于隐藏具体实现的组件.

案例:

// 声明一个接口
public interface demointerface{
 string hello(string text);
 // 一个hello方法,接收一个字符串型的参数,返回一个`string`类型.
}
// 实现
public class oneinterface implements demointerface{
 @override
 public string hello(string text){
  return "你好啊: " + text;
 }
}
// 测试类
public class main{
 public static void main(string[] args){
  demointerface demo = new oneinterface();
  system.out.println(demo.hello("dashucoding");
 }
}

什么是ioc,ioc是控制反转,那么什么控制反转,控制权的转移,应用程序不负责依赖对象的创建和维护,而是由外部容器负责创建和维护.

<?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 id="oneinterface" class="com.ioc.interfaces.oneinterfaceimpl"></bean>
</beans>

spring.xml

测试:

import org.junit.test;
@runwith(blockjunit4classrunner.class)
public class testoneinterface extends unittestbase {
 public testoneinterface(){
  super("spring.xml");
 }
 @test
 public void testhello(){
  oneinterface oneinterface = super.getbean("oneinterface");
  system.out.println(oneinterface.hello("dashucoding"));
 }
}

单元测试

下载一个包junit-*.jar导入项目中,然后创建一个unittestbase类,用于对spring进行配置文件的加载和销毁,所有的单元测试都是继承unittestbase的,然后通过它的getbean方法获取想要的对象,子类要加注解@runwith(blockjunit4classrunner.class),单元测试方法加注解@test.

 public classpathxmlapplicationcontext context;
 public string springxmlpath;
 public unittestbase(){}
 public unittestbase(string springxmlpath){
  this.springxmlpath = springxmlpath;
 }
@before
public void before(){
 if(stringutils.isempty(springxmlpath)){
  springxmlpath = "classpath*:spring-*.xml";
 }
 try{
  context = new classpathxmlapplicationcontext(springxmlpath.split("[,\\s]+"));
  context.start();
 }catch(beansexception e){
  e.printstacktrace();
 }
 }
 @after
 public void after(){
  context.destroy();
 }
 @suppresswarnings("unchecked")
 protected <t extends object> t getbean(string beanid){
  return (t)context.getbean(beanid);
 }
 protected <t extends object> t getbean(class<t> clazz){
  return context.getbean(clazz);
 }
}

bean容器:

org.springframework.beansorg.springframework.context
beanfactory提供配置结构和基本功能,加载并初始化bean,applicationcontext保存了bean对象.

// 文件
filesystemxmlapplicationcontext context = new filesystemxmlapplicationcontext("d:/appcontext.xml");
// classpath
classpathxmlapplicationcontext context = new classpathxmlapplicationcontext("classpath:spring-context.xml");
// web应用
<listener>
 <listener-class>org.springframework.web.context.contextloaderlistener</listener-class>
</listener>
<servlet>
 <servlet-name>context</servlet-name>
 <servlet-class>org.springframework.web.context.contextloaderservlet</servlet-class>
 <load-on-startup>1</load-on-startup>
</servlet>

spring注入:启动spring加载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 id="iservice" class="com.service.iserviceimpl">
  <property name="idao" ref="dao"/>
 </bean>
 <bean id="dao" class="com.idaoimpl"></bean>
</beans>
// 构造注入
<?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 id="iservice" class="com.service.iserviceimpl">
  <constructor-arg name="dao" ref="dao"/>
  <property name="injectiondao" ref="injectiondao"></property>
 </bean>
 <bean id="dao" class="com.idaoimpl"></bean>
</beans>

spring注入:

<?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 id="injectionservice" class="com.injection.service.injectionserviceimpl"></bean>
 <bean id="injectiondao" class="com.ijection.dao.injectiondaoimpl"></bean>
</beans>
// 接口-业务逻辑
public interface injectionservice {
 public void save(string arg);
}
// 实现类-处理业务逻辑
public class injectionserviceimpl implements injecionservice {
 private injectiondao injectiondao;
 public injectionserviceimpl(injectiondao injectiondao) {
  this.injectiondao = injectiondao;
 }
 public void setinjectiondao(injectiiondao injectiondao) {
  this.injectiondao = injectiondao;
 }
 public void save(string arg) {
  system.out.println("接收" + arg);
  arg = arg + ":" + this.hashcode();
  injectiondao.save(arg);
 }
}
// 接口-数据库-调用dao
public interface injectiondao { 
 // 声明一个方法
 public void save(string arg);
}
// 实现类
public class injectiondaoimpl implements injectiondao {
 // 实现接口中的方法
 public void save(string arg) {
  system.out.println("保存数据" + arg);
 }
}
// 测试
import org.junit.test;
@runwith(blockjunit4classrunner.class)
public class testinjection extends unittestbase {
 public testinjection(){
  super("classpath:spring-injection.xml");
 }
 @test
 public void  testsetter(){
  injectionservice service = super.getbean("injectionservice");
  service.save("保存的数据");
 }
 @test 
 public void testcons() {
  injectionservice service = super.getbean("injectionservice");
  service.save("保存的数据");
 }
}

bean的配置:

id:id是整个ioc容器中,bean的标识
class:具体要实例化的类
scope:作用域
constructor  arguments:构造器的参数
properties:属性
autowiring mode:自动装配的模式
lazy-initialization mode:懒加载模式
initialization/destruction method:初始化和销毁的方法

作用域

singleton:单例
prototype:每次请求都创建新的实例
request:每次http请求都创建一个实例有且当前有效
session:同上

spring bean配置之aware接口:spring中提供了以aware结尾的接口,为spring的扩展提供了方便.

bean的自动装配autowiring

no是指不做任何操作
byname是根据自己的属性名自动装配
bytype是指与指定属性类型相同的bean进行自动装配,如果有过个类型存在的bean,那么就会抛出异常,不能使用bytype方式进行自动装配,如果没有找到,就不什么事都不会发生
constructor是与bytype类似,它是用于构造器参数的,如果没有找到与构造器参数类型一致的bean就会抛出异常

spring bean配置的resource

resources:
urlresource是url的资源
classpathresource是获取类路径下的资源
filesystemresource是获取文件系统的资源
servletcontextresource是servletcontext封装的资源
inputstreamresource是针对输入流封装的资源
bytearrayresource是针对字节数组封装的资源
public interface resourceloader{
 resource getresource(string location);
}

resourceloader

classpath: loaded from the classpath;
file: loaded as a url, from the filesystem;
http: loaded as a url;

案例:

public class mresource implements applicationcontextaware{
  private applicationcontext applicationcontext;
 @override
 public void setapplicationcontext(applicationcontext applicationcontext) throws beansexception {
 this.applicationcontext = applicationcontext;
 }
 public void resource(){
  resource resource = applicationcontext.getresource("classpath:config.txt");
  system.out.println(resource.getfilename());
 }
}
// 单元测试类
import com.test.base.unittestbase;
@runwith(blockjunit4classrunner.class)
public class testresource extends unittestbase {
 public testresource() {
  super("classpath:spring-resource.xml");
 }
 @test
 public void testresource() {
  mresource resource = super.getbean("mresource");
  try{
   resource.resource();
  }catch(ioexception e){
   e.printstacktrace();
  }
 }
}
<?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 id="moocresource" class="com.resource.mresource"></bean>
</beans>

bean的定义与学习:

<context:annotation-config/>
@component,@repository,@service,@controller
@required,@autowired,@qualifier,@resource
@configuration,@bean,@import,@dependson
@component,@repository,@service,@controller
  1. @repository用于注解dao类为持久层
  2. @service用于注解service类为服务层
  3. @controller用于controller类为控制层

元注解meta-annotationsspring提供的注解可以作为字节的代码叫元数据注解,处理value(),元注解可以有其他的属性.

spring可以自动检测和注册bean

@service
public class simplemovielister {
 private moviefinder moviefinder;
 @autowired
 public simplemovielister(moviefinder moviefinder){
  this.moviefinder = moviefinder;
 }
}
@repository
public class jpamoviefinder implements moviefinder {

}

类的自动检测以及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">
 <context:component-scan base-package="org.example"/>
</beans>

类被自动发现并注册bean的条件:

用@component,@repository,@service,@controller注解或者使用@component的自定义注解

@required用于bean属性的setter方法
@autowired注解

private moviefinder moviefinder;
@autowired
public void setmoviefinder(moviefinder moviefinder) {
 this.moviefinder = moviefinder;
}
用于构造器或成员变量
@autowired
private moviecatalog moviecatalog;
private customepreferencedap customerpreferencedao;
@autowired
public movierecommender(customerpreferencedao customerpreferencedao) {
 this.customerpreferencedao = customerpreferencedao;
}

@autowired注解

使用这个注解,如果找不到bean将会导致抛出异常,可以使用下面代码避免,每个类只能有一个构造器被标记为required=true.

public class simplemovielister {
 private moviefinder moviefinder;
 @autowired(required=false)
 public void setmoviefinder(moviefinder moviefinder){
  this.moviefinder = moviefinder;
 }
}

spring是一个开源框架,spring是用j2ee开发的mvc框架,spring boot呢就是一个能整合组件的快速开发框架,因为使用maven管理,所以很便利。至于spring cloud,就是微服务框架了。

spring是一个轻量级的java开发框架,是为了解决企业应用开发的复杂性而创建的框架,框架具有分层架构的优势.

spring这种框架是简单性的,可测试性的和松耦合的,spring框架,我们主要是学习控制反转ioc和面向切面aop.

// 知识点
spring ioc
spring aop
spring orm
spring mvc
spring webservice
spring transactions
spring jms
spring data
spring cache
spring boot
spring security
spring schedule

spring ioc为控制反转,控制反向,控制倒置,

第65节:Java后端的学习之Spring基础

第65节:Java后端的学习之Spring基础

第65节:Java后端的学习之Spring基础

spring容器是 spring 框架的核心。spring容器实现了相互依赖对象的创建,协调工作,对象只要关系业务逻辑本身,ioc最重要的是完成了对象的创建和依赖的管理注入等,控制反转就是将代码里面需要实现的对象创建,依赖的代码,反转给了容器,这就需要创建一个容器,用来让容器知道创建对象与对象的关系.(告诉spring你是个什么东西,你需要什么东西)

xml,properties等用来描述对象与对象间的关系
classpath,filesystem,servletcontext等用来描述对象关系的文件放在哪里了.

控制反转就是将对象之间的依赖关系交给了容器管理,本来是由应用程序管理的对象之间的依赖的关系.

spring ioc体系结构

beanfactory
beandefinition

spring iocspring的核心之一,也是spring体系的基础,在spring中主要用户管理容器中的bean.springioc容器主要使用di方式实现的.beanfactory是典型的工厂模式,ioc容器为开发者管理对象间的依赖关系提供了很多便利.在使用对象时,要new object()来完成合作.ioc:spring容器是来实现这些相互依赖对象的创建和协调工作的.(由spring`来复杂控制对象的生命周期和对象间的)

所有的类的创建和销毁都是由spring来控制,不再是由引用它的对象了,控制对象的生命周期在spring.所有对象都被spring控制.

ioc容器的接口(自己设计和面对每个环节):

beanfactory工厂模式

public interface beanfactory {
 string factory_bean_prefix = "&"; 
 object getbean(string name) throws beansexception;
 object getbean(string name, class requiredtype) throws beansexception;

 boolean containsbean(string name); 
 boolean issingleton(string name) throws nosuchbeandefinitionexception;
 class gettype(string name) throws nosuchbeandefinitionexception;
 string[] getaliases(string name); 
}

beanfactory三个子类:listablebeanfactory,hierarchicalbeanfactoryautowirecapablebeanfactory,实现类是defaultlistablebeanfactory.

控制反转就是所有的对象都被spring控制.ioc动态的向某个对象提供它所需要的对象.通过di依赖注入来实现的.如何实现依赖注入id,在java中有一特性为反射,它可以在程序运行的时候进行动态的生成对象和执行对象的方法,改变对象的属性.

public static void main(string[] args){
 applicationcontext context = new filesystemxmlapplicationcontext("applicationcontext.xml");
 animal animal = (animal)context.getbean("animal");
 animal.say();
}
// applicationcontext.xml
<bean id="animal" class="com.test.cat">
 <property name="name" value="dashu"/>
</bean>
public class cat implements animal {
 private string name;
 public void say(){
  system.out.println("dashu");
 }
 public void setname(string name){
  this.name = name;
 }
}
public interface animal {
 public void say();
}
// bean
private string id;
private string type;
private map<string,object> properties=new hashmap<string, object>();
<bean id="test" class="test">
 <property name="testmap">

 </property>
</bean>
public static object newinstance(string classname) {
 class<?> cls = null;
 object obj = null;
 try {
  cls = class.forname(classname);
  obj = cls.newinstance();
 } catch (classnotfoundexception e) {
  throw new runtimeexception(e);
 } catch (instantiationexception e) {
  throw new runtimeexception(e);
 } catch (illegalaccessexception e) {
  throw new runtimeexception(e);
 }
 return obj;
}

核心是控制反转(ioc)和面向切面(aop),spring是一个分层的javase/ee的轻量级开源框架.

web:

struts,spring-mvc

service:

spring

dao:

mybatis,hibernate,jdbctemplate,springdata

spring体系结构

ioc

// 接口
public interface userservice {
 public void adduser();
}
// 实现类
public class userserviceimpl implements userservice {
 @override
 public void adduser(){
  system.out.println("dashucoding");
 }
}

配置文件:

<?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 id="userserviceid" class="com.dashucoding.userserviceimpl"></bean>
</beans>

测试:

@test
public void demo(){
    string xmlpath = "com/beans.xml";
    applicationcontext applicationcontext = new classpathxmlapplicationcontext(xmlpath);
    userservice userservice = (userservice) applicationcontext.getbean("userserviceid");
    userservice.adduser();
}

依赖注入:

class demoserviceimpl{
 private dadao dadao;
}

创建service实例,创建dao实例,将dao设置给service.

接口和实现类:

public interface bookdao {
    public void addbook();
}
public class bookdaoimpl implements bookdao {
    @override
    public void addbook() {
        system.out.println("dashucoding");
    }
}
public interface bookservice {
    public abstract void addbook();
}
public class bookserviceimpl implements bookservice {
    private bookdao bookdao;
    public void setbookdao(bookdao bookdao) {
        this.bookdao = bookdao;
    }
    @override
    public void addbook(){
        this.bookdao.addbook();
    }
}

配置文件:

<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 id="bookserviceid" class="com.bookserviceimpl">
        <property name="bookdao" ref="bookdaoid"></property>
    </bean>
    
    <bean id="bookdaoid" class="com.bookdaoimpl"></bean>
</beans>

测试:

@test
public void demo(){
    string xmlpath = "com/beans.xml";
    applicationcontext applicationcontext = new classpathxmlapplicationcontext(xmlpath);
    bookservice bookservice = (bookservice) applicationcontext.getbean("bookserviceid");
        
    bookservice.addbook();
}

ide建立spring项目

file—>new—>project—>spring

spring

// server.java
public class server {
 privete string name;
 public void setname(string name){
  this.name = name;
 }
 public void putname(){
  system.out.println(name);
 }
}
// main.java
public class main{
 public static void main(string[] args){
  applicationcontext context = new classpathxmlapplicationcontext("spring-config.xml");
  server hello = (server)context.getbean("example_one");
  hello.putname();
 }
}

spring-config.xml:

<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 id="example_one" class="server">
  <property name="name" value="达叔小生"></property>
 </bean>
</beans>

使用maven来声明spring库.maven是一个项目管理的工具,maven提供了开发人员构建一个完整的生命周期框架.maven的安装和配置,需要的是jdk 1.8,maven,windows,配置jdk,java_home变量添加到windows环境变量.下载apache maven,添加 m2_homemaven_home,添加到环境变量path,值为%m2_home%\bin.执行mvn –version命令来显示结果.

maven启用代理进行访问,找到文件路基,找到/conf/settings.xml,填写代理,要阿里的哦.

maven*存储库地址:
https://search.maven.org/

第65节:Java后端的学习之Spring基础

// xml
<dependency>
       <groupid>org.jvnet.localizer</groupid>
        <artifactid>localizer</artifactid>
        <version>1.8</version>
</dependency>
// pom.xml
<repositories>
    <repository>
        <id>java.net</id>
        <url>https://maven.java.net/content/repositories/public/</url>
    </repository>
</repositories>

maven添加远程仓库:

// pom.xml
<project ...>
<repositories>
    <repository>
      <id>java.net</id>
      <url>https://maven.java.net/content/repositories/public/</url>
    </repository>
 </repositories>
</project>

<project ...>
    <repositories>
      <repository>
    <id>jboss repository</id>
    <url>http://repository.jboss.org/nexus/content/groups/public/</url>
      </repository>
    </repositories>
</project>

maven依赖机制,使用maven创建java项目.

<dependency>
    <groupid>junit</groupid>
    <artifactid>junit</artifactid>
    <version>4.11</version>
    <scope>test</scope>
</dependency>

maven打包:

<project ...>
    <modelversion>4.0.0</modelversion>
    <groupid>com.dashucoding</groupid>
    <artifactid>numbergenerator</artifactid>    
    <packaging>jar</packaging>  
    <version>1.0-snapshot</version>

spring框架:

第65节:Java后端的学习之Spring基础

public interface helloworld{
 public void sayhello();
}
public class springhelloworld implements helloworld {
 public void sayhello(){
  system.out.println("spring hello");
 }
}

public class strutshelloworld implements helloworld {
 public void sayhello(){
  system.out.println("struts hello");
 }
}

public class helloworldservie {
 private helloworld helloworld;
 public helloworldservice(){
  this.helloworld = new strutshelloworld();
 }
}

控制反转:

public class helloworldservice{
 private helloworld helloworld;
 public helloworldservice(){
 }
 public void sethelloworld(helloworld helloworld){
  this.helloworld = helloworld;
 }
 public helloworld gethelloworld(){
  return this.helloworld;
 }
}

ioc创建了helloworldservice对象.

spring->helloprogram.java
helloworld->
helloworld.java
helloworldservice.java
impl实现类->
springhelloworld.java
strutshelloworld.java
resources->beans.xml
// 总结
一个spring:helloprogram.java
接口:
实现类:
资源:beans.xml
// helloworld.java
public interface helloworld {
 public void sayhello();
}
// public class helloworldservice {
 private helloworld helloworld;
 public helloworldservice(){
 }
 public void sethelloworld(helloworld helloworld){
  this.helloworld = helloworld;
 }
 public helloworld gethelloworld(){
  return this.helloworld;
 }
}
// springhelloworld.java
public class springhelloworld implements helloworld {
  
    @override
    public void sayhello() {
        system.out.println("spring hello!");
    }
}
// strutshelloworld.java
public class strutshelloworld implements helloworld {
  
    @override
    public void sayhello() {
        system.out.println("struts hello!");
    }
}
// helloprogram.java
public class helloprogram {
    public static void main(string[] args) {
        applicationcontext context =
                new classpathxmlapplicationcontext("beans.xml");
        helloworldservice service =
             (helloworldservice) context.getbean("helloworldservice");
        helloworld hw= service.gethelloworld();
        hw.sayhello();
    }
}
// beans.xml
<beansxmlns="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 id="springhelloworld"
        class="com.spring.helloworld.impl.springhelloworld"></bean>
    <bean id="strutshelloworld"
        class="com.spring.helloworld.impl.strutshelloworld"></bean>
  
  
    <bean id="helloworldservice"
        class="com.spring.helloworld.helloworldservice">
        <property name="helloworld" ref="springhelloworld"/>
    </bean>
  
</beans>
<propertyname="helloworld"ref="strutshelloworld"/>

ioc创建beans实现类springhelloworld,创建一个helloworldservice类,beans.xml实现参数导入:

// helloworldservice
// springhelloworld
// hello program.java
applicationcontext context = new classpathxmlapplicationcontxt("beans.xml");
helloworldservice service = (helloworldservice) context.getbean("helloworldservice");
helloworld hw = service.gethelloworld();
hw.sayhello();

// helloworldservice
public class helloworldservice {
 private helloworld helloworld;
 public helloworldservice(){
 }
 public void sethelloworld(helloworld helloworld){
  this.helloworld = helloworld;
 }
 public helloworld = gethelloworld() {
  return this.helloworld;
 }
}
// beans.xml
<bean id="名称" class="路径"/>
<bean id="helloworldservice"
 class="">
 <property name="helloworld" ref="springhelloworld"/>
</bean>

spring库地址:
http://maven.springframework.org/release/org/springframework/spring/

hello-world:

public class helloworld {
    private string name;
    public void setname(string name) {
        this.name = name;
    }
    public void printhello() {
        system.out.println("spring" + name);
    }
}
// xml
<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-3.0.xsd">

    <bean id="hellobean" class="">
        <property name="name" value="dashu" />
    </bean>

</beans>
// 执行
public class app {
    public static void main(string[] args) {
        applicationcontext context = new classpathxmlapplicationcontext(
                "applicationcontext.xml"); 
                helloworld obj = (helloworld) context.getbean("hellobean");
        obj.printhello();
    }
}

达叔小生:往后余生,唯独有你
you and me, we are family !
90后帅气小伙,良好的开发习惯;独立思考的能力;主动并且善于沟通
简书博客: 达叔小生

结语

  • 下面我将继续对 其他知识 深入讲解 ,有兴趣可以继续关注
  • 小礼物走一走 or 点赞