j-spring 资源注入(6)

88 阅读1分钟

前言

设计j-spring的时候没有把java的spring那一套搬过来,我只是想做一个IOC库,不想整框架那一套万精油。所以j-spring只支持手动注入配置参数,并且不支持EL表达式,及复杂属性的校验。这些工作留给第三方库吧!

代码实现

实现太过简单了,就直接走测试用例吧。 就是简单的向bean注入配置中的属性。

@Component
class Student {
    @Value({path:'student.name',type:String})
    name:String;
    @Value({path:'student.age',type:Number})
    age:20;
    @Value({path:'student.city',type:String})
    city:String
    
    getMsg(){
        return ''+this.name+this.age+this.city;
    }
}


@Component
class Application {

    @Value({path:'app.msg',type:String})
    appMsg:string;

    @Autowired({clazz:Student})
    student:Student;

    public main(){
        return `${this.appMsg}! my name is ${this.student.name} and ${this.student.age} years old!`;
    }

}


describe('resource config load test',()=>{

    it('A simple set value',()=>{

        spring.loadConfig({
            'app.msg':'hello',
            student:{
                name:'lina',
                age:20,
                city:'youda'
            }
        })

        expect(spring.launch(Application)).toEqual(`hello! my name is lina and 20 years old!`)

    })
})

总结

至此一个IOC库就完成了,剩下的就是开发j-springMvc。