spring4 学习1 javaproject
程序员文章站
2022-06-04 19:38:37
...
jar包下载地址:
http://maven.springframework.org/release/org/springframework/spring/4.2.4.RELEASE/
spring核心容器需要jar包5个,如果要通过在类上加注解的方式注入容器,则要添加spring-aop jar包
下面是工程引入的jar包:
源码如下:
package com.xx.demo;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class ClientDemoClass {
public static void main(String[] args) {
ApplicationContext context = new FileSystemXmlApplicationContext(
"src/config/application-context.xml");
DemoClass1 demo1 = (DemoClass1) context.getBean("demoClass1");
demo1.print();
}
}
package com.xx.demo;
import org.springframework.stereotype.Component;
@Component("demoClass1")
public class DemoClass1 {
public void print(){
System.out.println("这是DemoClass1");
}
}
<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-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<context:annotation-config />
<context:component-scan base-package="com.xx.demo" />
</beans>
DEBUG结果: