spring基础
Spring是一个开放源代码的设计层面框架,他解决的是业务逻辑层和其他各层的松耦合问题,因此它将面向接口的编程思想贯穿整个系统应用
IOC控制反转
把创建对象和维护对象之间的关系权利转交给spring管理,spring容器控制对象的创建,注入需要注入的对象
aop面向切面编程
通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术
隔离业务逻辑,降低耦合度,提高可用性和开发效率,主要用于日志记录,事务管理,异常处理等等
模块化
小demo
测试配置文件
创建对象
@Data
public class Person {
private String name;
}
创建配置文件,为了方便测试放在同级目录下
创建测试方法
public class SpringDemoTest {
@Test
public void tes01() {
//实例化管理对象
ClassPathResource resource = new ClassPathResource("/top/mjsmry/demo01/spring-config.xml");
XmlBeanFactory factory = new XmlBeanFactory(resource);
//1根据id创建
Person u = (Person) factory.getBean("person");
u.setName("lili");
System.out.println(u);
}
}
三种获取实例化的方式,上面已经写了一种
//2根据name和class
Person person = factory.getBean("person2", Person.class);
person.setName("lili");
//3直接根据字节码 但是配置文件必须唯一,一个对象配置多个bean的话,spring会无法选择
Person bean = factory.getBean(Person.class);
System.out.println(person);
Application和BeanFactory
- 创建对象的时机是
- ApplicationContext在创建工厂的时候就会把bean实例化,优先加载
- BeanFactory使用对象时才会实例化
- 关系
- ApplicationContext实现BeanFactory
- ApplicationContext更强大
- ApplicationContext实现懒加载
- 全局:default-lazy-init="true"
单独节点: lazy-init="true"
spring测试
spring测试可以在不启动spring项目情况下进行单元测试
@ContextConfiguration("spring-config.xml")
@RunWith(SpringJUnit4ClassRunner.class)
public class SpringTest {
@Autowired
Person person;
@Test
public void test() {
person.setName("lili");
System.out.println(person);
}
}
singletonAndprototype
scope prototype多例 singleton单例
public class SpringTest {
@Test
public void test() {
ApplicationContext ac=new ClassPathXmlApplicationContext("top/mjsmry/singletonAndprototype/spring-config.xml");
Person person1= (Person) ac.getBean("person");
Person person2= (Person) ac.getBean("person");
//单例的话就是同一个对象
System.out.println(person1==person2);//true
}
}
bean与bean之间的关系
继承
- 通过parent指定继承
- 有abstract属性的bean不能被实例化
子bean可以覆盖父bean
依赖
- 可以通过depends-on="其他beani定依赖关系
- 如果依赖了abstract的bean也不能实例化
@Test
public void test() {
ApplicationContext ac=new ClassPathXmlApplicationContext("top/mjsmry/beanAndBean/spring-config.xml");
Person person= (Person) ac.getBean("person");
System.out.println(person);
Person person2= (Person) ac.getBean("person2");
System.out.println(person2);
}
生命周期
基本声明周期
bean
@Data
public class Person {
private String name;
public Person() {
System.out.println("构造方法调用了");
}
private void init() {
System.out.println("--init--");
}
private void destory() {
System.out.println("--destory--");
}
}
补充周期
实现接口
public class PersonBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object o, String s) throws BeansException {
System.out.println("postProcessBeforeInitialization");
return o;
}
@Override
public Object postProcessAfterInitialization(Object o, String s) throws BeansException {
System.out.println("postProcessAfterInitialization");
return o;
}
}
测试
@Test
public void test() {
ClassPathXmlApplicationContext ca=new ClassPathXmlApplicationContext("top/mjsmry/beancycle/spring-config.xml");
Person person= (Person) ca.getBean("person");
ca.close();
}
注入测试
spring原生注入方式实现三层架构
dao
public class TestDao {
public int add() {
return 1;
}
}
service
public interface TestService {
String add();
}
@Data
public class TestServiceImpl implements TestService {
private TestDao testDao;
@Override
public String add() {
return testDao.add()==1?"添加成功":"添加失败";
}
}
controller
@Data
public class TestController {
private TestService testService;
public String add() {
return testService.add();
}
}
配置文件
test
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/spring-config.xml")
public class MyTest {
@Autowired
private TestController testController;
@Test
public void test01() {
String result = testController.add();
System.out.println(result);
}
}
测试结果
注入BasicDataSource
db.properties
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/jdbcwork?useSSL=false&serverTimezone=UTC
jdbc.username=xxx
jdbc.password=xxx
测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("spring-config.xml")
public class DataSourceTest {
@Autowired
BasicDataSource basicDataSource;
@Test
public void test01() {
try {
Connection connection = basicDataSource.getConnection();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
无报错测试通过
其他注入
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Person {
private String username;
private Car car;
private String[] strings;
private List list;
private Set set;
private List wifeList;
private Properties p1;
private Properties p2;
}
1
2
3
k
e
w
proKey1=proValue1
值1
测试
public class OtherTest {
@Test
public void test01() {
ApplicationContext ac=new ClassPathXmlApplicationContext("/top/mjsmry/_05other/spring-config.xml");
Person person= (Person) ac.getBean("person");
Person person2= (Person) ac.getBean("person2");
Person person3= (Person) ac.getBean("person3");
System.out.println(person);
System.out.println(person2);
System.out.println(person3);
}
}
关注我的个人博客林中小屋
更多内容关注我的个人博客林中小屋