Spring的核心基础:感受一下对象工厂

“欢迎来到Spring!”的小项目

(1)写一个HelloSpring的类,采用setter方法注入userName,写一个简单的show方法。

package com.itzhoutao;
public class  HelloSpring{
	private String userName;

	public void setUserName(String userName)
	{
		this.userName = userName;
	}

	public void show(){
		System.out.println(userName + ":欢迎来到Spring!");
	}

}

(2)利用Spring容器,如ApplicationContext,初始化容器,加载xml配置,获取(getBean)实例。写了一个TestHelloSpring类。

package com.itzhoutao;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestHelloSpring {
    public static void main(String[] args) {
        //初始化spring容器,加载applicationContext.xml配置
        ApplicationContext applicationContext = new
                ClassPathXmlApplicationContext("applicationContext.xml");
        //通过容器获取配置中helloSpring的实例
        HelloSpring helloSpring =
                (HelloSpring) applicationContext.getBean("helloSpring");
        helloSpring.show();//调用方法
    }

}

3.applicatiomContext.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!-- 将指定类配置给Spring,让Spring创建HelloSpring对象的实例 -->
    <bean id="helloSpring" class="com.itzhoutao.HelloSpring">
        <!--为userName属性赋值-->
        <property name="userName" value="土豆哥哥"></property>
    </bean>
</beans>

手动管理j的ar包和这个项目的基本结构:

jar包在springlibs目录下,相关程序代码写在了work目录下。

手动编译运行Spring项目的方法:

1. 将Spring所需的所有jar包(含commons-logging的jar包)文件放到C:\Program Files\Java\jre1.8.0_181\lib\ext的目录下,并另外存放一份放到源码所在位置的springlibs目录下。

2. 将所有的.java源码文件放到某个工作文件夹比如work中(注意只要.java的文件,不要包名对应的文件夹),将所需的xml配置文件也拷贝到本文件夹中。

3. 进入work工作文件夹,然后在命令行下运行编译命令:javac -d . *.java -encoding utf8 -classpath ..\springlibs\* 即可成功编译。

4. 运行:在work工作文件夹中,运行命令:java 主类的包名.类名。

相关推荐

  1. spring核心详解

    2024-07-11 06:14:03       27 阅读
  2. Spring 核心注解

    2024-07-11 06:14:03       19 阅读
  3. jsString对象

    2024-07-11 06:14:03       36 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-11 06:14:03       53 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-11 06:14:03       55 阅读
  3. 在Django里面运行非项目文件

    2024-07-11 06:14:03       46 阅读
  4. Python语言-面向对象

    2024-07-11 06:14:03       56 阅读

热门阅读

  1. Flutter RSA公钥转PEM

    2024-07-11 06:14:03       21 阅读
  2. CentOS 系统监控项

    2024-07-11 06:14:03       19 阅读
  3. UCOS-III 与UCOS-III主要功能差异

    2024-07-11 06:14:03       14 阅读
  4. 用 adb 来模拟手机插上电源和拔掉电源的情形

    2024-07-11 06:14:03       19 阅读
  5. OpenResty程序如何连接开启了TLS的Redis?

    2024-07-11 06:14:03       22 阅读
  6. Jitsi Meet指定用户成为主持人

    2024-07-11 06:14:03       17 阅读
  7. Rust编程-编写自动化测试

    2024-07-11 06:14:03       24 阅读
  8. 开源大势所趋

    2024-07-11 06:14:03       21 阅读
  9. Sqlmap中文使用手册 - Target模块参数使用

    2024-07-11 06:14:03       23 阅读
  10. Grind 75 - Leetcode146 LRU缓存

    2024-07-11 06:14:03       23 阅读