前言:笔记是参考B站up主遇见狂神说,图片、代码都是哦。因为最近特别喜欢他教的课程,所以就一边跟着学习,一边在blog写笔记~(图片、代码来源狂神说Java,侵权必删!) 狂神说Java学习路线B站网站:https://www.bilibili.com/read/cv5702420
IOC理论推导
Dao层
1.UserDao 接口 2.UserDaoImpl 实现类
Service层
3.UserService 业务接口 4.UserServiceImpl 业务实现类 用户实际调用service层 不会动dao层!
Dao层接口创建
package com.ckm.dao;
public interface UserDao {
void getUser();
}
Dao层实现类1创建
package com.ckm.dao;
public class UserDaoImpl implements UserDao{
@Override
public void getUser() {
System.out.println("获取了User数据");
}
}
Dao层实现类2创建
package com.ckm.dao;
public class UserDaoMysqlImpl implements UserDao{
@Override
public void getUser() {
System.out.println("mysql使用了数据");
}
}
Service层接口创建
package com.ckm.service;
public interface UserService {
void getUser();
}
Service层接口实现类创建
package com.ckm.service;
import com.ckm.dao.UserDao;
import com.ckm.dao.UserDaoImpl;
public class UserServiceImpl implements UserService{
private UserDao userDao;
public void setUserDao(UserDao userDao){
this.userDao = userDao;
}
@Override
public void getUser() {
userDao.getUser();
}
}
单元测试
import com.ckm.dao.UserDaoImpl;
import com.ckm.dao.UserDaoMysqlImpl;
import com.ckm.service.UserServiceImpl;
public class TestIOC {
public static void main(String[] args) {
UserServiceImpl userService = new UserServiceImpl();
userService.setUserDao(new UserDaoMysqlImpl());
userService.getUser();
}
}
Spring管理对象
resources中新建beans.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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="DaoImpl" class="com.ckm.dao.UserDaoImpl" />
<bean id="MysqlImpl" class="com.ckm.dao.UserDaoMysqlImpl" />
<bean id="UserServiceImpl" class="com.ckm.service.UserServiceImpl">
<property name="userDao" ref="MysqlImpl" />
</bean>
</beans>
dao层、Service层代码不变 测试类
import com.ckm.service.UserServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestIOC {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
UserServiceImpl userServiceImpl = (UserServiceImpl) context.getBean("UserServiceImpl");
userServiceImpl.getUser();
}
}
Spring管理对象的简单例子
User类
package com.ckm.pojo;
public class Hello {
private String str;
public String getStr() {
return str;
}
public void setStr(String str) {
this.str = str;
}
@Override
public String toString() {
return "Hello{" +
"str='" + str + '\'' +
'}';
}
}
beans.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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="hello" class="com.ckm.pojo.Hello">
<property name="str" value="Spring"/>
</bean>
</beans>
测试类
import com.ckm.pojo.Hello;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Hello hello = (Hello) context.getBean("hello");
System.out.println(hello.getStr());
}
}
Bean无参构造类创建和有参构造类创建
当getBean的时候,这个类就已经被实例化了,就会执行无参构造方法 有参构造的User类
package com.ckm.pojo;
public class User {
private String name;
public User(String name){
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void show(){
System.out.println("name="+name);
}
}
编写Beans.xml
<bean id="user" class="com.ckm.pojo.User">
<constructor-arg index="0" value="这个是第一种有参设置的name"/>
</bean>
<bean id="user" class="com.ckm.pojo.User">
<constructor-arg name="name" value="这个是第二种方法是有参设置的name"/>
</bean>
Spring的一些配置
别名
<alias name="user" alias="userNew"/>
Bean的配置
<bean id="user" class="com.ckm.pojo.User" name="user1,user2,user3,user4"/>
import
<import resource="beans1.xml"/>
<import resource="beans2.xml"/>
|