spring是一个管家,将对象创建和调用的任务交给它去打理
建立实体类对象user
public class user {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
在resources中新建beans.xml
<bean id="user" class="com.ly.spring5.User" >bean>
Spring创建对象默认无参构造
<bean id="book" class="com.ly.spring5.Book">
<property name="bname" value="骆驼祥子">property>
<property name="bname" value="孔乙己">property>
bean>
<bean id="order" class="com.ly.spring5.Order">
<constructor-arg name="oname" value="电脑">constructor-arg>
<constructor-arg name="oaddress" value="china">constructor-arg>
bean>
xmlns:p="http://www.springframework.org/schema/p"
<bean id="book" class="com.ly.spring5.Book" p:bauthor="ly" p:bname="九阳神功">bean>
/*Dao类实现类*/
package com.ly.spring5.dao;
public class UserDaoImpl implements UserDao{
@Override
public void update() {
System.out.println("dao update........");
}
}
/*service类*/
package com.ly.spring5.service;
import com.ly.spring5.dao.UserDao;
import com.ly.spring5.dao.UserDaoImpl;
public class UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void add(){
System.out.println("service add......");
userDao.update();
}
}
/*测试类*/
@Test
public void testAdd(){
//加载Spring配置文件
ApplicationContext context = new FileSystemXmlApplicationContext("src/bean2.xml");
//获取配置创建对象
UserService userService = context.getBean("userService", UserService.class);
userService.add();
}
1、注入数组类型属性
2、注入List集合类型属性
3、注入Map集合类型属性
package com.ly.spring5.collectiontype;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class Stu {
//数组类型属性
private String[] courses;
//List集合类型属性
private List<String> list;
//map集合类型属性
private Map<String,String> maps;
//set集合类型属性
private Set<String> sets;
public void setSets(Set<String> sets) {
this.sets = sets;
}
public void setList(List<String> list) {
this.list = list;
}
public void setMaps(Map<String, String> maps) {
this.maps = maps;
}
public void setCourses(String[] courses) {
this.courses = courses;
}
}
<bean id="stu" class="com.ly.spring5.collectiontype.Stu">
<property name="courses">
<array>
<value>JAVAvalue>
<value>数据库value>
<value>Springvalue>
<value>WEBvalue>
array>
property>
<property name="list">
<list>
<value>张三value>
<value>小三value>
list>
property>
<property name="maps">
<map>
<entry key="JAVA" value="java">entry>
<entry key="PHP" value="php">entry>
map>
property>
<property name="sets">
<set>
<value>Mysqlvalue>
<value>Redisvalue>
set>
property>
bean>
<bean id="emp" class="com.ly.spring5.autowire.Emp" autowire="byName">
bean>
<bean id="dept" class="com.ly.spring5.autowire.Dept">bean>
<context:property-path path="src/jdbc.properties">context:property-path>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driverClass">property>
<property name="url" value="${jdbc.url}">property>
<property name="username" value="${jdbc.username}">property>
<property name="password" value="${jdbc.password}">property>
bean>
jdbc.driverClass=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/book?useSSL=FALSE&serverTimezone=UTC
jdbc.username=root
jdbc.password=root
<context:componenet-scan base-package="com.ly">context:componenet-scan>
package com.ly.spring5.service;
import org.springframework.stereotype.Component;
//在注解里面value属性值可以省略不写
//默认值是类名称,首字母小写
@Component(value = "userService") //等同于xml开发中的
public class UserService {
public void add(){
System.out.println("service add ....");
}
}
public class demo1 {
@Test
public void testService(){
ApplicationContext context = new FileSystemXmlApplicationContext("src/bean1.xml");
UserService userService = context.getBean("userService", UserService.class);
System.out.println(userService);
userService.add();
}
}
package com.ly.spring5.dao;
import org.springframework.stereotype.Repository;
@Repository
public class UserDaoImpl implements UserDao{
@Override
public void add() {
System.out.println("dao add....");
}
}
@Service
public class UserService {
//定义dao类属性
//不需要set方法
//添加注入属性注解
@Autowired //根据类型进行注入
private UserDao userDao;
public void add(){
System.out.println("service add ....");
userDao.add();
}
}
@Configuration //作为配置类,替代xml配置文件
@ComponentScan(basePackages = {"com.ly"})
public class SpringConfig {
}
@Testpublic
void testService2(){
//加载配置类
ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
UserService userService = context.getBean("userService", UserService.class);
System.out.println(userService);
userService.add();
}