在myEclipse上运行
1.配置好Maven的环境变量
2.创建Maven项目,在pom.xml里面编写要下载的包
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>edu.edu.edu</groupId>
<artifactId>es</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
3.在类路径下编写配置文件
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd" >
<!-- 找到包下的所有子包,当有Component时装入类 -->
<context:component-scan base-package="edu"></context:component-scan>
</beans>
<context:component-scan base-package="edu"></context:component-scan>是遍历该报下的所有子包里的类,当有@Component时,就配置该类,默认id为类名,name为包名加类名
4.写类
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class User {
@Value("张三")
private String name;
@Value("12")
private int id;
@Value("男")
private String sex;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
@Value()给变量赋值
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
//@Component("stu")
//以下三个@Component的三个衍生注解:
//@Repository("stu") //持久层
//@Service("stu") //业务层
@Controller("stu") //WEB层
//以上这几种都可以
@Scope("prototype") //重新一个类,遇到相同的类重新分配地址
public class UserDao {
@Value("西安") //赋值
private String a;
@Value("北京")
private String d;
@Autowired //调用另外一个类(自动装配)
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String getA() {
return a;
}
public void setA(String a) {
this.a = a;
}
public String getD() {
return d;
}
public void setD(String d) {
this.d = d;
}
}
4.执行配置里的类
public class Test1 {
@Test
public void test1(){
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
UserDao u = ac.getBean(UserDao.class);
System.out.println(u.getA()+" :"+u.getD());
System.out.println(u.getUser().getId()+" "+u.getUser().getName()+" "+u.getUser().getSex());
UserDao u1 = ac.getBean(UserDao.class);
System.out.println(u==u1);
UserDao u2 = (UserDao) ac.getBean("stu"); //@Component("stu")
System.out.println(u==u2);
System.out.println(u.getUser()==u1.getUser());
}
}
如果不做任何处理,每次给相同类分配的地址相同。
但是给类前面加@Scope("prototype")则每次分配内存的地址不同