提交 5a6390b1 编写于 作者: 武汉红喜's avatar 武汉红喜

JavaConfig

上级 e0144282
......@@ -32,7 +32,7 @@
</modules>
<properties>
<spring.version>4.0.0.RELEASE</spring.version>
<spring.version>4.3.7.RELEASE</spring.version>
<ibatis.version>3.2.7</ibatis.version>
<rootdir>${basedir}</rootdir>
</properties>
......
......@@ -15,10 +15,6 @@
<name>${project.artifactId}</name>
<url>http://maven.apache.org</url>
<properties>
<spring.version>3.2.11.RELEASE</spring.version>
</properties>
<!-- 依赖包 -->
<dependencies>
<dependency>
......
......@@ -18,10 +18,12 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
......
package com.itlong.whatsmars.spring.annotation;
import com.itlong.whatsmars.spring.model.Mars;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* Created by javahongxi on 2017/10/31.
*/
@Configuration
public class AppConfig {
@Bean
public Mars mars() {
return new Mars(1000, "火星");
}
}
package com.itlong.whatsmars.spring.annotation;
import com.itlong.whatsmars.spring.model.Mars;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* Created by javahongxi on 2017/10/31.
*/
public class TestSpring {
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(AppConfig.class);
ctx.refresh();
Mars mars = ctx.getBean(Mars.class);
System.out.println(mars.getCnName());
}
}
package com.itlong.whatsmars.spring.bean;
package com.itlong.whatsmars.spring.factory;
import com.itlong.whatsmars.spring.Mars;
import com.itlong.whatsmars.spring.model.Mars;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.Resource;
......
package com.itlong.whatsmars.spring;
package com.itlong.whatsmars.spring.model;
/**
* Created by shenhongxi on 2016/4/7.
......@@ -9,6 +9,13 @@ public class Mars {
private String cnName;
public Mars() {}
public Mars(long age, String cnName) {
this.age = age;
this.cnName = cnName;
}
public long getAge() {
return age;
}
......
package com.itlong.whatsmars.spring;
package com.itlong.whatsmars.spring.xml;
import com.itlong.whatsmars.spring.model.Mars;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.ContextLoaderListener;
import javax.servlet.ServletContextListener;
/**
* Created by shenhongxi on 2016/4/7.
......@@ -15,12 +13,5 @@ public class TestSpring {
Mars mars = (Mars) context.getBean("mars");
System.out.println(mars.getAge());
System.out.println(mars.getCnName());
//ServletContextListener contextListener = new ContextLoaderListener();
String s = "http://jd.com";
System.out.println(s.substring(5, s.length()));
Integer n = 3;
System.out.println("3".equals(n));
}
}
......@@ -8,9 +8,9 @@
http://www.springframework.org/schema/context/spring-context-3.2.xsd"
default-autowire="byName">
<context:component-scan base-package="com.itlong.whatsmars.spring"/>
<!--<context:component-scan base-package="com.itlong.whatsmars.spring"/>-->
<bean id="mars" class="com.itlong.whatsmars.spring.Mars">
<bean id="mars" class="com.itlong.whatsmars.spring.model.Mars">
<property name="age" value="45" />
<property name="cnName" value="火星" />
</bean>
......
package com.itlong.whatsmars.spring;
import com.itlong.whatsmars.spring.model.Mars;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册