asp.netとspringの統合のもう一つの考え方
詳細
プロジェクトのため、Javaから.NETに移行し、C#には素人です.
現在asp.netとspring.netの統合では、注入するbeanをweb.configに配置する必要があり、かなり不快なので、自家製ホイールを用意しています.
実験の考え方:
一般的に業務ロジックを扱うコードをaspxページから個別に分離してPageから継承するClassとなるため,この場所からすべての注入が必要なbeanをclassファイルに定義し,効率を向上させるためにInjectAttributeマークアップが注入されるSetメソッドをカスタマイズすることができ,以下は実験過程である.
実験クラス:
InjectカスタムAttributeと表記されたクラスのメソッドを実験的に取得します.
コンソールから出力すると、プロジェクトのメソッドを正しく取得でき、次のステップでは必要に応じて注入されたbeanNameをspingから取得し、注入することができます.
プロジェクトのため、Javaから.NETに移行し、C#には素人です.
現在asp.netとspring.netの統合では、注入するbeanをweb.configに配置する必要があり、かなり不快なので、自家製ホイールを用意しています.
実験の考え方:
一般的に業務ロジックを扱うコードをaspxページから個別に分離してPageから継承するClassとなるため,この場所からすべての注入が必要なbeanをclassファイルに定義し,効率を向上させるためにInjectAttributeマークアップが注入されるSetメソッドをカスタマイズすることができ,以下は実験過程である.
[AttributeUsage(AttributeTargets.Method)]
public class InjectAttribute : Attribute {
private String beanName;
public InjectAttribute(String beanName) {
this.beanName = beanName;
}
public String BeanName {
get { return this.beanName; }
}
}
実験クラス:
public class PageInfo {
private String userName;
private String password;
[Inject("balas")]
public void SetUserName(String userName) {
this.userName = userName;
}
[Inject("pwd")]
public void SetPassword(String password) {
this.password = password;
}
public String UserName {
get { return this.userName; }
}
}
InjectカスタムAttributeと表記されたクラスのメソッドを実験的に取得します.
public class Program {
static void Main(string[] args) {
PageInfo page = new PageInfo();
page.SetUserName("keven chen");
Console.WriteLine("Hello World,"+page.UserName);
Type type = page.GetType();
MethodInfo[] methods = type.GetMethods();
for (int i = 0; i < methods.Length; i++) {
Boolean find = false;
foreach (Attribute attr in methods[i].GetCustomAttributes(typeof(InjectAttribute), false)) {
if (attr is InjectAttribute) {
find = true;
Console.WriteLine(((InjectAttribute)attr).BeanName);
break;
}
}
if (find) {
Console.WriteLine(methods[i].Name);
}
}
MethodInfo method = type.GetMethod("SetUserName");
if (null != method) {
method.Invoke(page, new object[] {"bribin"});
}
Console.WriteLine(page.UserName);
}
}
コンソールから出力すると、プロジェクトのメソッドを正しく取得でき、次のステップでは必要に応じて注入されたbeanNameをspingから取得し、注入することができます.