LEEYANGY

LEEYANGY 关注TA

拼搏百天,我要上湖工大!

LEEYANGY

LEEYANGY

关注TA

拼搏百天,我要上湖工大!

  •  Wuhan/HuBei
  • 学生
  • 写了322,476字

该文章投稿至Nemo社区   Java  板块 复制链接


spring入门笔记

发布于 2021/07/23 22:57 15,955浏览 0回复 6,457

暂时无更新

代码更新至github


官方文档

在idea创建maven工程

删除现有的src目录,新建module ,spring-01-ioc1 

打开项目Project根目录下的pom.xml ,在里面</project>标签之前添加spring支持

5.2.0目前已经不是最新,根据自己需求变更版本。。。。

<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
</dependencies>

在spring-01-ioc1中src-main-java中新建包top.leeyangy.dao和top.leeyangy.service

dao包中新建UserDao接口类,

public interface UserDao {
void getUser();
}

UserDaoImpl,

public class UserDaoImpl implements UserDao {
public void getUser() {
System.out.println("获取用户数据");
}
}

UserDaoMysqlImpl,

public class UserDaoMysqlImpl implements UserDao {
@Override
public void getUser() {
System.out.println("MySQL获取用户数据");
}
}

resources目录下新建:

bean.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="..." class="...">
<!-- collaborators and configuration for this bean go here -->
</bean>

<bean id="..." class="...">
<!-- collaborators and configuration for this bean go here -->
</bean>

<!-- more bean definitions go here -->

</beans>

然后改成如下内容(注释仅供参考):

<?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">

<!--
spring 数据基本类型
赋值用value
引用类型ref赋值
ref 引用Spring容器中创建好的对象
-->

<!-- 注册-->
<bean id="mysqlImpl" class="top.leeyangy.dao.UserDaoMysqlImpl"/>
<!-- collaborators and configuration for this bean go here -->
<bean id="UserServiceImpl" class="top.leeyangy.service.UserServiceImpl">
<!-- ref 应用spring容器中创建好的对象
value 具体的值 基本数据类型
-->
<property name="userDao" ref="mysqlImpl"/>
</bean>
</beans>

在test目录下新建MyTest类(直接psvm)

//    @Test
public static void main(String[] args) {
// UserServiceImpl userService = new UserServiceImpl();
// userService.getUser();
// 获取ApplicationContex
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
// 需要什么直接get什么
UserServiceImpl userServiceImpl = (UserServiceImpl) context.getBean("UserServiceImpl");
userServiceImpl.getUser();
}

运行MyTest

得到结果:

MySQL获取用户数据

代码分析:

稍后更新-------还在研究spring-helloworld


-----------------华丽分割线---------------------


自动装配Bean

<bean id="cat" class="top.leeyangy.pojo.Cat"/>
<bean id="dog" class="top.leeyangy.pojo.Dog"/>

<!-- <bean id="people" class="top.leeyangy.pojo.People">-->
<!-- <property name="name" value="LEEYANGY"/>-->
<!-- <property name="cat" ref="cat"/>-->
<!-- <property name="dog" ref="dog"/>-->
<!-- </bean>-->

<!-- byName-->
<bean id="people" class="top.leeyangy.pojo.People" autowire="byName">
<property name="name" value="LEEYANGY"/>
</bean>

<!-- byType-->
<!-- <bean id="cat" class="top.leeyangy.pojo.Cat"/>-->
<!-- <bean id="dog111" class="top.leeyangy.pojo.Dog"/>-->
<!-- <bean id="people" class="top.leeyangy.pojo.People" autowire="byType">-->
<!-- <property name="name" value="LEEYANGY"/>-->
<!-- </bean>-->

<!-- byName 需要保证所有bean id 唯一 ,并且bean需要和自动注入的属性set方法值一致-->
<!-- byType 需要保证bean class 唯一 ,并且这个bean需要和自动注入的属性set方法的值一致 -->

<!-- 1.导入spring坐标spring-context 对应版本-->

<!-- 2.配置bean
bean标签配置bean
id 属性标签给bean起名字(id用来配置bean名称)
class 属性表示当前给bean定义类型(配置bean的全路径类名)
-->
<!--
bean作用范围
默认为单例,如需配置多例
表现层,业务,数据,工具对象可以交给spring管理
封装实体域对象,不适合给spring

bean本质上就是对象,创建bean使用构造方法完成

<bean id="bookService" class="xyz.leeyangy.service.impl.BookServiceImpl">
配置service与dao的关系-->
<!-- property表示配置当前bean属性
name属性配置哪一个具体的属性
ref 参照哪一个bean
-->
<!-- <property name="bookDao" ref="bookDao"/>-->
<!-- </bean>-->


在使用自动装配的时候需要手动添加约束

<?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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!-- 注解支持-->
<context:annotation-config/>

</beans>


在beans插入之前的代码

    <bean id="cat" class="top.leeyangy.pojo.Cat"/>
<bean id="dog" class="top.leeyangy.pojo.Dog"/>
<bean id="people" class="top.leeyangy.pojo.People"/>

<?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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!-- 注解支持-->
<context:annotation-config/>

<bean id="cat" class="top.leeyangy.pojo.Cat"/>
<bean id="dog" class="top.leeyangy.pojo.Dog"/>
<bean id="people" class="top.leeyangy.pojo.People"/>


</beans>

@Autowired  直接在属性上使用,在属性上添加@Autowired注解  可以不用写set方法,前提是自动装配的属性在IOC容器中存在,且符合名字byName

。。。。。。

注解开发

。。。。。。。





点赞(0)
点了个评