1.创建对象
<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"></bean> = AccountServiceImpl accountService = new com.itheima.service.impl.AccountServiceImpl();
2.赋值(依赖注入)
1.set方法注入(赋值)值
调用set方法赋值
例子:
<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"> <!-- property:表示使用属性注入 调用setName给name赋值 name="":表示要调用的set方法,省略set,第一个字母小写 value="小红红":赋值,value只针对基本数据类型和字符串 --> </a> <!-- age --> <property name="age" value="22" /> <!-- birthday ref="date":如果要赋值的数据是一个Bean 对象,则使用ref引用 --> <property name="birthday" ref="date" /> </bean>
2.构造函数注入(赋值)
调用带参数的构造函数给指定属性赋值
例子:
<!-- 构造函数注入 --> <bean id="accountService2" class="com.itheima.service.impl.AccountServiceImpl"> <!-- 构造函数注入 name="指定构造函数的入参名字" --> <constructor-arg name="name" value="小王" /> <constructor-arg name="age" value="25" /> <constructor-arg name="birthday" ref="date" /> </bean>
3.注解注入
涉及到的注解,巧记十八罗汉↓
@ComponentScan @Component @Controller @Service @Repository @Autowired @Qualifier @Resource @Value @Scope @PostConstruct @PreDestroy @ComponentScan @Bean @Import @Configuration @PropertySource @RunWith @ContextConfiguration
DBUtils(会用)
操作数据库的框架 QueryRunner:操作数据库的对象,用于执行SQL语句->ResultSet。 QueryRunner runner = new QueryRunner(dataSource); 需要注入数据源 单个结果:BeanHandler->JavaBean Account account = runner.query(sql, new BeanHandler<Account>(Account.class), 3); 多个结果:BeanListHandler->List<JavaBean> List<Account> list = runner.query(sql, new BeanListHandler<Account>(Account.class));
DBUtils增删改查
增删改:queryRunner.update() 查询:queryRunner.query()
Spring集成DBUtils
Dao层 ->xml-><bean class="xx" /> Service层 ->xml-><bean class="xx" /> DataSource ->xml-><bean class="xx" /> QueryRunner ->xml-><bean class="xx" />
案例
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 创建DataSource --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <!--注入4个属性--> <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/spring5" /> <property name="driverClass" value="com.mysql.jdbc.Driver" /> <property name="user" value="root" /> <property name="password" value="itcast" /> </bean> <!-- 创建QueryRunner的Bean --> <bean id="queryRunner" class="org.apache.commons.dbutils.QueryRunner"> <!--构造函数注入--> <constructor-arg name="ds" ref="dataSource" /> </bean> <!-- Dao --> <bean id="accountDao" class="com.itheima.dao.impl.AccountDaoImpl"> <!-- 注入QueryRunner --> <property name="queryRunner" ref="queryRunner" /> </bean> <!-- Service --> <bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"> <!--注入AccountDao--> <property name="accountDao" ref="accountDao" /> </bean> </beans>
创建对象注解
@Component(value="accountService") <bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl" /> @Component:相当于创建了对象的实例,并将对象交给Spring容器管理,注解中value值用于定义对象实例的唯一标识,如果不写value值,则默认值为类的名字,第一个字母小写
4个创建对象的注解
@Component(value = "accountService") 用于标注普通类 @Controller(value = "accountService") web->控制层->@Controller @Service(value = "accountService") 业务逻辑->@Service @Repository(value = "accountService") Dao->数据仓库->@Repository
配置包扫描,告诉Spring对应的注解所在的包
<!-- 包扫描 多个包需要扫描,可以用逗号隔开:com.itheima.service.impl,com.itheima.dao.impl base-package="com.itheima":扫描com.itheima包以及它下面的所有子包 --> <context:component-scan base-package="com.itheima" />
依赖注入注解
@Autowired 作用:实现依赖注入(不依靠set方法、不依靠带参构造函数,原理是反射) 装配(注入)方式: 1.根据类型注入 byType 2.根据类型注入如果存在多个对应类型,则根据名字注入 byName @Qualifier(value = "accountDao") 不能单独使用,主要作用是指定@Autowired注入的Bean对象 @Resource 作用:实现依赖注入 装配方式: 1.默认根据名字注入 byName 2.如果名字注入找不到对应的Bean,则根据类型注入 byType @Value 作用:实现[基本数据类型和字符串]依赖注入 主要用于将配置文件中的数据注入到JavaBean的属性中 jdbc.properties ${}:表示加载指定的配置文件中对应的key的值,如果不加${}则表示直接将值赋值给属性,${}是一个表达式使用
xml换成纯注解
1.创建类(配置类)取代xml文件 @Configuration:配置类 2.将xml中的配置逐步取代,将相关的配置写入到配置类中
包扫描
@ComponentScan(basePackages = {"com.itheima.service","com.itheima.dao"})
AnnotationConfigApplicationContext
AnnotationConfigApplicationContext:用于注解创建Spring容器对象 参数:配置类的字节码对象,可以是多个
@Import
@Import(JdbcConfig.class),引入另外一个配置类,把jq文件拖到我的代码里面
@PropertySource
@PropertySource(value = "classpath:jdbc.properties") 表示加载读取类路径下的jdbc.properties文件
@Bean
@Bean:将方法的返回值交给SpringIOC容器(ApplicationContext)对象管理 Bean的ID默认为方法名字 @Bean(name = "dataSource"),这里的name用于自定义Bean的ID 如果该注解所在的方法有参数传入,则会从容器中注入进来 注入方式: 默认byType 类型注入失败,则byName注入
SpringTest,可以在测试类中直接使用Spring注解
1.导入依赖包,使用SpringTest功能 2.@RunWith注解 集成JUnit,创建Spring TestContextManager容器对象 3.@ContextConfiguration 指定配置文件或者配置类
案例:
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes =SpringConfig.class )