Spring--入门第一天【Bean其实就是一个new好的对象】

本文介绍了Spring入门的基本概念,包括通过类路径和文件系统加载配置文件,使用BeanFactory,以及如何在XML中装配Bean。详细阐述了实例化Bean的三种方式,并涉及了bean的作用域、SpEL表达式、依赖注入的各种方法如构造器注入、set注入及p命名空间注入。此外,还提供了测试环节以加深理解。
摘要由CSDN通过智能技术生成

加载Spring容器的方式

类路径获得配置文件

ApplicationContext context = new ClassPathXmlApplicationContext("com/gyf/spring/demo01/beans.xml");

文件系统路径获得配置文件【绝对路径】

使用BeanFactory(了解)

配置文件相应包导入

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
"></beans>

项目目录 

装配Bean(xml)

实例化Bean的三种方式

<!--    new实现类-->
    <bean id="userServiceByID" name="userServiceByName" class="com.pratice.service.Impl.UserServiceImpl"></bean>
<!--工厂模式装配bean spring的版本过低,3.0版本,把jdk改成1.7-->
<!--    静态方法-->
    <bean id="user" class="com.pratice.bean.factory.CreateUserFactory" factory-method="getUser2"></bean>
<!--工厂实例-->
    <bean id="factory" class="com.pratice.bean.factory.CreateUserFactory"></bean>
    <bean id="user2" factory-bean="factory" factory-method="getUser1"></bean>

测试

package com.pratice.test;

import com.pratice.bean.User;
import com.pratice.controller.UserController;
import com.pratice.service.Impl.UserServiceImpl;
import com.pratice.service.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestSpring {
    /
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值